blob: 6f121a904d05e5850168373211f3e97ccb77dd09 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
Kashyap, Desai19d3ebe2009-09-14 11:01:36 +05305 * Copyright (C) 2007-2009 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +053055#include <linux/raid_class.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060057
58#include "mpt2sas_base.h"
59
60MODULE_AUTHOR(MPT2SAS_AUTHOR);
61MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
62MODULE_LICENSE("GPL");
63MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
64
65#define RAID_CHANNEL 1
66
67/* forward proto's */
68static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
69 struct _sas_node *sas_expander);
70static void _firmware_event_work(struct work_struct *work);
71
72/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060073LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060074
75/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060076static u8 scsi_io_cb_idx = -1;
77static u8 tm_cb_idx = -1;
78static u8 ctl_cb_idx = -1;
79static u8 base_cb_idx = -1;
80static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053081static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060082static u8 config_cb_idx = -1;
83static int mpt_ids;
84
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053085static u8 tm_tr_cb_idx = -1 ;
86static u8 tm_sas_control_cb_idx = -1;
87
Eric Moore635374e2009-03-09 01:21:12 -060088/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060089static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060090MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
91 "(default=0)");
92
93/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
94#define MPT2SAS_MAX_LUN (16895)
95static int max_lun = MPT2SAS_MAX_LUN;
96module_param(max_lun, int, 0);
97MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
98
99/**
100 * struct sense_info - common structure for obtaining sense keys
101 * @skey: sense key
102 * @asc: additional sense code
103 * @ascq: additional sense code qualifier
104 */
105struct sense_info {
106 u8 skey;
107 u8 asc;
108 u8 ascq;
109};
110
111
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530112#define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
113
Eric Moore635374e2009-03-09 01:21:12 -0600114/**
115 * struct fw_event_work - firmware event struct
116 * @list: link list framework
117 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530118 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600119 * @ioc: per adapter object
120 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530121 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600122 * @ignore: flag meaning this event has been marked to ignore
123 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
124 * @event_data: reply event data payload follows
125 *
126 * This object stored on ioc->fw_event_list.
127 */
128struct fw_event_work {
129 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530130 u8 cancel_pending_work;
131 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600132 struct MPT2SAS_ADAPTER *ioc;
133 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530134 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600135 u8 ignore;
136 u16 event;
137 void *event_data;
138};
139
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530140/* raid transport support */
141static struct raid_template *mpt2sas_raid_template;
142
Eric Moore635374e2009-03-09 01:21:12 -0600143/**
144 * struct _scsi_io_transfer - scsi io transfer
145 * @handle: sas device handle (assigned by firmware)
146 * @is_raid: flag set for hidden raid components
147 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
148 * @data_length: data transfer length
149 * @data_dma: dma pointer to data
150 * @sense: sense data
151 * @lun: lun number
152 * @cdb_length: cdb length
153 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600154 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530155 * @VF_ID: virtual function id
156 * @VP_ID: virtual port id
157 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600158 * @sense_length: sense length
159 * @ioc_status: ioc status
160 * @scsi_state: scsi state
161 * @scsi_status: scsi staus
162 * @log_info: log information
163 * @transfer_length: data length transfer when there is a reply message
164 *
165 * Used for sending internal scsi commands to devices within this module.
166 * Refer to _scsi_send_scsi_io().
167 */
168struct _scsi_io_transfer {
169 u16 handle;
170 u8 is_raid;
171 enum dma_data_direction dir;
172 u32 data_length;
173 dma_addr_t data_dma;
174 u8 sense[SCSI_SENSE_BUFFERSIZE];
175 u32 lun;
176 u8 cdb_length;
177 u8 cdb[32];
178 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530179 u8 VF_ID;
180 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600181 u8 valid_reply;
182 /* the following bits are only valid when 'valid_reply = 1' */
183 u32 sense_length;
184 u16 ioc_status;
185 u8 scsi_state;
186 u8 scsi_status;
187 u32 log_info;
188 u32 transfer_length;
189};
190
191/*
192 * The pci device ids are defined in mpi/mpi2_cnfg.h.
193 */
194static struct pci_device_id scsih_pci_table[] = {
195 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
196 PCI_ANY_ID, PCI_ANY_ID },
197 /* Falcon ~ 2008*/
198 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
199 PCI_ANY_ID, PCI_ANY_ID },
200 /* Liberator ~ 2108 */
201 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
202 PCI_ANY_ID, PCI_ANY_ID },
203 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
204 PCI_ANY_ID, PCI_ANY_ID },
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
206 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530207 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600208 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
209 PCI_ANY_ID, PCI_ANY_ID },
210 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
211 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530212 /* Thunderbolt ~ 2208 */
213 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
214 PCI_ANY_ID, PCI_ANY_ID },
215 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
216 PCI_ANY_ID, PCI_ANY_ID },
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
220 PCI_ANY_ID, PCI_ANY_ID },
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
222 PCI_ANY_ID, PCI_ANY_ID },
223 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
224 PCI_ANY_ID, PCI_ANY_ID },
225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
226 PCI_ANY_ID, PCI_ANY_ID },
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
228 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600229 {0} /* Terminating entry */
230};
231MODULE_DEVICE_TABLE(pci, scsih_pci_table);
232
233/**
Eric Moored5d135b2009-05-18 13:02:08 -0600234 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600235 *
236 * Note: The logging levels are defined in mpt2sas_debug.h.
237 */
238static int
Eric Moored5d135b2009-05-18 13:02:08 -0600239_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600240{
241 int ret = param_set_int(val, kp);
242 struct MPT2SAS_ADAPTER *ioc;
243
244 if (ret)
245 return ret;
246
247 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600248 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600249 ioc->logging_level = logging_level;
250 return 0;
251}
Eric Moored5d135b2009-05-18 13:02:08 -0600252module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600253 &logging_level, 0644);
254
255/**
256 * _scsih_srch_boot_sas_address - search based on sas_address
257 * @sas_address: sas address
258 * @boot_device: boot device object from bios page 2
259 *
260 * Returns 1 when there's a match, 0 means no match.
261 */
262static inline int
263_scsih_srch_boot_sas_address(u64 sas_address,
264 Mpi2BootDeviceSasWwid_t *boot_device)
265{
266 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
267}
268
269/**
270 * _scsih_srch_boot_device_name - search based on device name
271 * @device_name: device name specified in INDENTIFY fram
272 * @boot_device: boot device object from bios page 2
273 *
274 * Returns 1 when there's a match, 0 means no match.
275 */
276static inline int
277_scsih_srch_boot_device_name(u64 device_name,
278 Mpi2BootDeviceDeviceName_t *boot_device)
279{
280 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
281}
282
283/**
284 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
285 * @enclosure_logical_id: enclosure logical id
286 * @slot_number: slot number
287 * @boot_device: boot device object from bios page 2
288 *
289 * Returns 1 when there's a match, 0 means no match.
290 */
291static inline int
292_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
293 Mpi2BootDeviceEnclosureSlot_t *boot_device)
294{
295 return (enclosure_logical_id == le64_to_cpu(boot_device->
296 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
297 SlotNumber)) ? 1 : 0;
298}
299
300/**
301 * _scsih_is_boot_device - search for matching boot device.
302 * @sas_address: sas address
303 * @device_name: device name specified in INDENTIFY fram
304 * @enclosure_logical_id: enclosure logical id
305 * @slot_number: slot number
306 * @form: specifies boot device form
307 * @boot_device: boot device object from bios page 2
308 *
309 * Returns 1 when there's a match, 0 means no match.
310 */
311static int
312_scsih_is_boot_device(u64 sas_address, u64 device_name,
313 u64 enclosure_logical_id, u16 slot, u8 form,
314 Mpi2BiosPage2BootDevice_t *boot_device)
315{
316 int rc = 0;
317
318 switch (form) {
319 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
320 if (!sas_address)
321 break;
322 rc = _scsih_srch_boot_sas_address(
323 sas_address, &boot_device->SasWwid);
324 break;
325 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
326 if (!enclosure_logical_id)
327 break;
328 rc = _scsih_srch_boot_encl_slot(
329 enclosure_logical_id,
330 slot, &boot_device->EnclosureSlot);
331 break;
332 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
333 if (!device_name)
334 break;
335 rc = _scsih_srch_boot_device_name(
336 device_name, &boot_device->DeviceName);
337 break;
338 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
339 break;
340 }
341
342 return rc;
343}
344
345/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530346 * _scsih_get_sas_address - set the sas_address for given device handle
347 * @handle: device handle
348 * @sas_address: sas address
349 *
350 * Returns 0 success, non-zero when failure
351 */
352static int
353_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
354 u64 *sas_address)
355{
356 Mpi2SasDevicePage0_t sas_device_pg0;
357 Mpi2ConfigReply_t mpi_reply;
358 u32 ioc_status;
359
360 if (handle <= ioc->sas_hba.num_phys) {
361 *sas_address = ioc->sas_hba.sas_address;
362 return 0;
363 } else
364 *sas_address = 0;
365
366 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
367 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
368 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
369 ioc->name, __FILE__, __LINE__, __func__);
370 return -ENXIO;
371 }
372
373 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
374 MPI2_IOCSTATUS_MASK;
375 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
376 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
377 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
378 __FILE__, __LINE__, __func__);
379 return -EIO;
380 }
381
382 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
383 return 0;
384}
385
386/**
Eric Moore635374e2009-03-09 01:21:12 -0600387 * _scsih_determine_boot_device - determine boot device.
388 * @ioc: per adapter object
389 * @device: either sas_device or raid_device object
390 * @is_raid: [flag] 1 = raid object, 0 = sas object
391 *
392 * Determines whether this device should be first reported device to
393 * to scsi-ml or sas transport, this purpose is for persistant boot device.
394 * There are primary, alternate, and current entries in bios page 2. The order
395 * priority is primary, alternate, then current. This routine saves
396 * the corresponding device object and is_raid flag in the ioc object.
397 * The saved data to be used later in _scsih_probe_boot_devices().
398 */
399static void
400_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
401 void *device, u8 is_raid)
402{
403 struct _sas_device *sas_device;
404 struct _raid_device *raid_device;
405 u64 sas_address;
406 u64 device_name;
407 u64 enclosure_logical_id;
408 u16 slot;
409
410 /* only process this function when driver loads */
411 if (!ioc->wait_for_port_enable_to_complete)
412 return;
413
414 if (!is_raid) {
415 sas_device = device;
416 sas_address = sas_device->sas_address;
417 device_name = sas_device->device_name;
418 enclosure_logical_id = sas_device->enclosure_logical_id;
419 slot = sas_device->slot;
420 } else {
421 raid_device = device;
422 sas_address = raid_device->wwid;
423 device_name = 0;
424 enclosure_logical_id = 0;
425 slot = 0;
426 }
427
428 if (!ioc->req_boot_device.device) {
429 if (_scsih_is_boot_device(sas_address, device_name,
430 enclosure_logical_id, slot,
431 (ioc->bios_pg2.ReqBootDeviceForm &
432 MPI2_BIOSPAGE2_FORM_MASK),
433 &ioc->bios_pg2.RequestedBootDevice)) {
434 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
435 "%s: req_boot_device(0x%016llx)\n",
436 ioc->name, __func__,
437 (unsigned long long)sas_address));
438 ioc->req_boot_device.device = device;
439 ioc->req_boot_device.is_raid = is_raid;
440 }
441 }
442
443 if (!ioc->req_alt_boot_device.device) {
444 if (_scsih_is_boot_device(sas_address, device_name,
445 enclosure_logical_id, slot,
446 (ioc->bios_pg2.ReqAltBootDeviceForm &
447 MPI2_BIOSPAGE2_FORM_MASK),
448 &ioc->bios_pg2.RequestedAltBootDevice)) {
449 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
450 "%s: req_alt_boot_device(0x%016llx)\n",
451 ioc->name, __func__,
452 (unsigned long long)sas_address));
453 ioc->req_alt_boot_device.device = device;
454 ioc->req_alt_boot_device.is_raid = is_raid;
455 }
456 }
457
458 if (!ioc->current_boot_device.device) {
459 if (_scsih_is_boot_device(sas_address, device_name,
460 enclosure_logical_id, slot,
461 (ioc->bios_pg2.CurrentBootDeviceForm &
462 MPI2_BIOSPAGE2_FORM_MASK),
463 &ioc->bios_pg2.CurrentBootDevice)) {
464 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
465 "%s: current_boot_device(0x%016llx)\n",
466 ioc->name, __func__,
467 (unsigned long long)sas_address));
468 ioc->current_boot_device.device = device;
469 ioc->current_boot_device.is_raid = is_raid;
470 }
471 }
472}
473
474/**
475 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
476 * @ioc: per adapter object
477 * @sas_address: sas address
478 * Context: Calling function should acquire ioc->sas_device_lock
479 *
480 * This searches for sas_device based on sas_address, then return sas_device
481 * object.
482 */
483struct _sas_device *
484mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
485 u64 sas_address)
486{
487 struct _sas_device *sas_device, *r;
488
489 r = NULL;
490 /* check the sas_device_init_list */
491 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
492 list) {
493 if (sas_device->sas_address != sas_address)
494 continue;
495 r = sas_device;
496 goto out;
497 }
498
499 /* then check the sas_device_list */
500 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
501 if (sas_device->sas_address != sas_address)
502 continue;
503 r = sas_device;
504 goto out;
505 }
506 out:
507 return r;
508}
509
510/**
511 * _scsih_sas_device_find_by_handle - sas device search
512 * @ioc: per adapter object
513 * @handle: sas device handle (assigned by firmware)
514 * Context: Calling function should acquire ioc->sas_device_lock
515 *
516 * This searches for sas_device based on sas_address, then return sas_device
517 * object.
518 */
519static struct _sas_device *
520_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
521{
522 struct _sas_device *sas_device, *r;
523
524 r = NULL;
525 if (ioc->wait_for_port_enable_to_complete) {
526 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
527 list) {
528 if (sas_device->handle != handle)
529 continue;
530 r = sas_device;
531 goto out;
532 }
533 } else {
534 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
535 if (sas_device->handle != handle)
536 continue;
537 r = sas_device;
538 goto out;
539 }
540 }
541
542 out:
543 return r;
544}
545
546/**
547 * _scsih_sas_device_remove - remove sas_device from list.
548 * @ioc: per adapter object
549 * @sas_device: the sas_device object
550 * Context: This function will acquire ioc->sas_device_lock.
551 *
552 * Removing object and freeing associated memory from the ioc->sas_device_list.
553 */
554static void
555_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
556 struct _sas_device *sas_device)
557{
558 unsigned long flags;
559
560 spin_lock_irqsave(&ioc->sas_device_lock, flags);
561 list_del(&sas_device->list);
562 memset(sas_device, 0, sizeof(struct _sas_device));
563 kfree(sas_device);
564 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
565}
566
567/**
568 * _scsih_sas_device_add - insert sas_device to the list.
569 * @ioc: per adapter object
570 * @sas_device: the sas_device object
571 * Context: This function will acquire ioc->sas_device_lock.
572 *
573 * Adding new object to the ioc->sas_device_list.
574 */
575static void
576_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
577 struct _sas_device *sas_device)
578{
579 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600580
581 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
582 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
583 sas_device->handle, (unsigned long long)sas_device->sas_address));
584
585 spin_lock_irqsave(&ioc->sas_device_lock, flags);
586 list_add_tail(&sas_device->list, &ioc->sas_device_list);
587 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
588
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530589 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
590 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600591 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600592}
593
594/**
595 * _scsih_sas_device_init_add - insert sas_device to the list.
596 * @ioc: per adapter object
597 * @sas_device: the sas_device object
598 * Context: This function will acquire ioc->sas_device_lock.
599 *
600 * Adding new object at driver load time to the ioc->sas_device_init_list.
601 */
602static void
603_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
604 struct _sas_device *sas_device)
605{
606 unsigned long flags;
607
608 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
609 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
610 sas_device->handle, (unsigned long long)sas_device->sas_address));
611
612 spin_lock_irqsave(&ioc->sas_device_lock, flags);
613 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
614 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
615 _scsih_determine_boot_device(ioc, sas_device, 0);
616}
617
618/**
Eric Moore635374e2009-03-09 01:21:12 -0600619 * _scsih_raid_device_find_by_id - raid device search
620 * @ioc: per adapter object
621 * @id: sas device target id
622 * @channel: sas device channel
623 * Context: Calling function should acquire ioc->raid_device_lock
624 *
625 * This searches for raid_device based on target id, then return raid_device
626 * object.
627 */
628static struct _raid_device *
629_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
630{
631 struct _raid_device *raid_device, *r;
632
633 r = NULL;
634 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
635 if (raid_device->id == id && raid_device->channel == channel) {
636 r = raid_device;
637 goto out;
638 }
639 }
640
641 out:
642 return r;
643}
644
645/**
646 * _scsih_raid_device_find_by_handle - raid device search
647 * @ioc: per adapter object
648 * @handle: sas device handle (assigned by firmware)
649 * Context: Calling function should acquire ioc->raid_device_lock
650 *
651 * This searches for raid_device based on handle, then return raid_device
652 * object.
653 */
654static struct _raid_device *
655_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
656{
657 struct _raid_device *raid_device, *r;
658
659 r = NULL;
660 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
661 if (raid_device->handle != handle)
662 continue;
663 r = raid_device;
664 goto out;
665 }
666
667 out:
668 return r;
669}
670
671/**
672 * _scsih_raid_device_find_by_wwid - raid device search
673 * @ioc: per adapter object
674 * @handle: sas device handle (assigned by firmware)
675 * Context: Calling function should acquire ioc->raid_device_lock
676 *
677 * This searches for raid_device based on wwid, then return raid_device
678 * object.
679 */
680static struct _raid_device *
681_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
682{
683 struct _raid_device *raid_device, *r;
684
685 r = NULL;
686 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
687 if (raid_device->wwid != wwid)
688 continue;
689 r = raid_device;
690 goto out;
691 }
692
693 out:
694 return r;
695}
696
697/**
698 * _scsih_raid_device_add - add raid_device object
699 * @ioc: per adapter object
700 * @raid_device: raid_device object
701 *
702 * This is added to the raid_device_list link list.
703 */
704static void
705_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
706 struct _raid_device *raid_device)
707{
708 unsigned long flags;
709
710 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
711 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
712 raid_device->handle, (unsigned long long)raid_device->wwid));
713
714 spin_lock_irqsave(&ioc->raid_device_lock, flags);
715 list_add_tail(&raid_device->list, &ioc->raid_device_list);
716 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
717}
718
719/**
720 * _scsih_raid_device_remove - delete raid_device object
721 * @ioc: per adapter object
722 * @raid_device: raid_device object
723 *
724 * This is removed from the raid_device_list link list.
725 */
726static void
727_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
728 struct _raid_device *raid_device)
729{
730 unsigned long flags;
731
732 spin_lock_irqsave(&ioc->raid_device_lock, flags);
733 list_del(&raid_device->list);
734 memset(raid_device, 0, sizeof(struct _raid_device));
735 kfree(raid_device);
736 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
737}
738
739/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530740 * mpt2sas_scsih_expander_find_by_handle - expander device search
741 * @ioc: per adapter object
742 * @handle: expander handle (assigned by firmware)
743 * Context: Calling function should acquire ioc->sas_device_lock
744 *
745 * This searches for expander device based on handle, then returns the
746 * sas_node object.
747 */
748struct _sas_node *
749mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
750{
751 struct _sas_node *sas_expander, *r;
752
753 r = NULL;
754 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
755 if (sas_expander->handle != handle)
756 continue;
757 r = sas_expander;
758 goto out;
759 }
760 out:
761 return r;
762}
763
764/**
Eric Moore635374e2009-03-09 01:21:12 -0600765 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
766 * @ioc: per adapter object
767 * @sas_address: sas address
768 * Context: Calling function should acquire ioc->sas_node_lock.
769 *
770 * This searches for expander device based on sas_address, then returns the
771 * sas_node object.
772 */
773struct _sas_node *
774mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
775 u64 sas_address)
776{
777 struct _sas_node *sas_expander, *r;
778
779 r = NULL;
780 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
781 if (sas_expander->sas_address != sas_address)
782 continue;
783 r = sas_expander;
784 goto out;
785 }
786 out:
787 return r;
788}
789
790/**
791 * _scsih_expander_node_add - insert expander device to the list.
792 * @ioc: per adapter object
793 * @sas_expander: the sas_device object
794 * Context: This function will acquire ioc->sas_node_lock.
795 *
796 * Adding new object to the ioc->sas_expander_list.
797 *
798 * Return nothing.
799 */
800static void
801_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
802 struct _sas_node *sas_expander)
803{
804 unsigned long flags;
805
806 spin_lock_irqsave(&ioc->sas_node_lock, flags);
807 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
808 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
809}
810
811/**
812 * _scsih_is_end_device - determines if device is an end device
813 * @device_info: bitfield providing information about the device.
814 * Context: none
815 *
816 * Returns 1 if end device.
817 */
818static int
819_scsih_is_end_device(u32 device_info)
820{
821 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
822 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
823 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
824 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
825 return 1;
826 else
827 return 0;
828}
829
830/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530831 * mptscsih_get_scsi_lookup - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600832 * @ioc: per adapter object
833 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600834 *
835 * Returns the smid stored scmd pointer.
836 */
837static struct scsi_cmnd *
838_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
839{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530840 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600841}
842
843/**
844 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
845 * @ioc: per adapter object
846 * @smid: system request message index
847 * @scmd: pointer to scsi command object
848 * Context: This function will acquire ioc->scsi_lookup_lock.
849 *
850 * This will search for a scmd pointer in the scsi_lookup array,
851 * returning the revelent smid. A returned value of zero means invalid.
852 */
853static u16
854_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
855 *scmd)
856{
857 u16 smid;
858 unsigned long flags;
859 int i;
860
861 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
862 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530863 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600864 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530865 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600866 goto out;
867 }
868 }
869 out:
870 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
871 return smid;
872}
873
874/**
875 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
876 * @ioc: per adapter object
877 * @id: target id
878 * @channel: channel
879 * Context: This function will acquire ioc->scsi_lookup_lock.
880 *
881 * This will search for a matching channel:id in the scsi_lookup array,
882 * returning 1 if found.
883 */
884static u8
885_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
886 int channel)
887{
888 u8 found;
889 unsigned long flags;
890 int i;
891
892 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
893 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530894 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600895 if (ioc->scsi_lookup[i].scmd &&
896 (ioc->scsi_lookup[i].scmd->device->id == id &&
897 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
898 found = 1;
899 goto out;
900 }
901 }
902 out:
903 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
904 return found;
905}
906
907/**
Eric Moore993e0da2009-05-18 13:00:45 -0600908 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
909 * @ioc: per adapter object
910 * @id: target id
911 * @lun: lun number
912 * @channel: channel
913 * Context: This function will acquire ioc->scsi_lookup_lock.
914 *
915 * This will search for a matching channel:id:lun in the scsi_lookup array,
916 * returning 1 if found.
917 */
918static u8
919_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
920 unsigned int lun, int channel)
921{
922 u8 found;
923 unsigned long flags;
924 int i;
925
926 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
927 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530928 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600929 if (ioc->scsi_lookup[i].scmd &&
930 (ioc->scsi_lookup[i].scmd->device->id == id &&
931 ioc->scsi_lookup[i].scmd->device->channel == channel &&
932 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
933 found = 1;
934 goto out;
935 }
936 }
937 out:
938 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
939 return found;
940}
941
942/**
Eric Moore635374e2009-03-09 01:21:12 -0600943 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
944 * @ioc: per adapter object
945 * @smid: system request message index
946 *
947 * Returns phys pointer to chain buffer.
948 */
949static dma_addr_t
950_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
951{
952 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
953 ioc->chains_needed_per_io));
954}
955
956/**
957 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
958 * @ioc: per adapter object
959 * @smid: system request message index
960 *
961 * Returns virt pointer to chain buffer.
962 */
963static void *
964_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
965{
966 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
967 ioc->chains_needed_per_io)));
968}
969
970/**
971 * _scsih_build_scatter_gather - main sg creation routine
972 * @ioc: per adapter object
973 * @scmd: scsi command
974 * @smid: system request message index
975 * Context: none.
976 *
977 * The main routine that builds scatter gather table from a given
978 * scsi request sent via the .queuecommand main handler.
979 *
980 * Returns 0 success, anything else error
981 */
982static int
983_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
984 struct scsi_cmnd *scmd, u16 smid)
985{
986 Mpi2SCSIIORequest_t *mpi_request;
987 dma_addr_t chain_dma;
988 struct scatterlist *sg_scmd;
989 void *sg_local, *chain;
990 u32 chain_offset;
991 u32 chain_length;
992 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +0900993 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -0600994 u32 sges_in_segment;
995 u32 sgl_flags;
996 u32 sgl_flags_last_element;
997 u32 sgl_flags_end_buffer;
998
999 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1000
1001 /* init scatter gather flags */
1002 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1003 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1004 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1005 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1006 << MPI2_SGE_FLAGS_SHIFT;
1007 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1008 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1009 << MPI2_SGE_FLAGS_SHIFT;
1010 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1011
1012 sg_scmd = scsi_sglist(scmd);
1013 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001014 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001015 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1016 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1017 return -ENOMEM;
1018 }
1019
1020 sg_local = &mpi_request->SGL;
1021 sges_in_segment = ioc->max_sges_in_main_message;
1022 if (sges_left <= sges_in_segment)
1023 goto fill_in_last_segment;
1024
1025 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1026 (sges_in_segment * ioc->sge_size))/4;
1027
1028 /* fill in main message segment when there is a chain following */
1029 while (sges_in_segment) {
1030 if (sges_in_segment == 1)
1031 ioc->base_add_sg_single(sg_local,
1032 sgl_flags_last_element | sg_dma_len(sg_scmd),
1033 sg_dma_address(sg_scmd));
1034 else
1035 ioc->base_add_sg_single(sg_local, sgl_flags |
1036 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1037 sg_scmd = sg_next(sg_scmd);
1038 sg_local += ioc->sge_size;
1039 sges_left--;
1040 sges_in_segment--;
1041 }
1042
1043 /* initializing the chain flags and pointers */
1044 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1045 chain = _scsih_get_chain_buffer(ioc, smid);
1046 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1047 do {
1048 sges_in_segment = (sges_left <=
1049 ioc->max_sges_in_chain_message) ? sges_left :
1050 ioc->max_sges_in_chain_message;
1051 chain_offset = (sges_left == sges_in_segment) ?
1052 0 : (sges_in_segment * ioc->sge_size)/4;
1053 chain_length = sges_in_segment * ioc->sge_size;
1054 if (chain_offset) {
1055 chain_offset = chain_offset <<
1056 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1057 chain_length += ioc->sge_size;
1058 }
1059 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1060 chain_length, chain_dma);
1061 sg_local = chain;
1062 if (!chain_offset)
1063 goto fill_in_last_segment;
1064
1065 /* fill in chain segments */
1066 while (sges_in_segment) {
1067 if (sges_in_segment == 1)
1068 ioc->base_add_sg_single(sg_local,
1069 sgl_flags_last_element |
1070 sg_dma_len(sg_scmd),
1071 sg_dma_address(sg_scmd));
1072 else
1073 ioc->base_add_sg_single(sg_local, sgl_flags |
1074 sg_dma_len(sg_scmd),
1075 sg_dma_address(sg_scmd));
1076 sg_scmd = sg_next(sg_scmd);
1077 sg_local += ioc->sge_size;
1078 sges_left--;
1079 sges_in_segment--;
1080 }
1081
1082 chain_dma += ioc->request_sz;
1083 chain += ioc->request_sz;
1084 } while (1);
1085
1086
1087 fill_in_last_segment:
1088
1089 /* fill the last segment */
1090 while (sges_left) {
1091 if (sges_left == 1)
1092 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1093 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1094 else
1095 ioc->base_add_sg_single(sg_local, sgl_flags |
1096 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1097 sg_scmd = sg_next(sg_scmd);
1098 sg_local += ioc->sge_size;
1099 sges_left--;
1100 }
1101
1102 return 0;
1103}
1104
1105/**
Eric Moored5d135b2009-05-18 13:02:08 -06001106 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001107 * @sdev: scsi device struct
1108 * @qdepth: requested queue depth
Mike Christiee881a172009-10-15 17:46:39 -07001109 * @reason: calling context
Eric Moore635374e2009-03-09 01:21:12 -06001110 *
1111 * Returns queue depth.
1112 */
1113static int
Mike Christiee881a172009-10-15 17:46:39 -07001114_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
Eric Moore635374e2009-03-09 01:21:12 -06001115{
1116 struct Scsi_Host *shost = sdev->host;
1117 int max_depth;
1118 int tag_type;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301119 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1120 struct MPT2SAS_DEVICE *sas_device_priv_data;
1121 struct MPT2SAS_TARGET *sas_target_priv_data;
1122 struct _sas_device *sas_device;
1123 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001124
Mike Christiee881a172009-10-15 17:46:39 -07001125 if (reason != SCSI_QDEPTH_DEFAULT)
1126 return -EOPNOTSUPP;
1127
Eric Moore635374e2009-03-09 01:21:12 -06001128 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301129
1130 /* limit max device queue for SATA to 32 */
1131 sas_device_priv_data = sdev->hostdata;
1132 if (!sas_device_priv_data)
1133 goto not_sata;
1134 sas_target_priv_data = sas_device_priv_data->sas_target;
1135 if (!sas_target_priv_data)
1136 goto not_sata;
1137 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1138 goto not_sata;
1139 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1140 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1141 sas_device_priv_data->sas_target->sas_address);
1142 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1143 if (sas_device && sas_device->device_info &
1144 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1145 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1146
1147 not_sata:
1148
Eric Moore635374e2009-03-09 01:21:12 -06001149 if (!sdev->tagged_supported)
1150 max_depth = 1;
1151 if (qdepth > max_depth)
1152 qdepth = max_depth;
1153 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1154 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1155
1156 if (sdev->inquiry_len > 7)
1157 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1158 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1159 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1160 sdev->ordered_tags, sdev->scsi_level,
1161 (sdev->inquiry[7] & 2) >> 1);
1162
1163 return sdev->queue_depth;
1164}
1165
1166/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301167 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001168 * @sdev: scsi device struct
1169 * @tag_type: requested tag type
1170 *
1171 * Returns queue tag type.
1172 */
1173static int
Eric Moored5d135b2009-05-18 13:02:08 -06001174_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001175{
1176 if (sdev->tagged_supported) {
1177 scsi_set_tag_type(sdev, tag_type);
1178 if (tag_type)
1179 scsi_activate_tcq(sdev, sdev->queue_depth);
1180 else
1181 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1182 } else
1183 tag_type = 0;
1184
1185 return tag_type;
1186}
1187
1188/**
Eric Moored5d135b2009-05-18 13:02:08 -06001189 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001190 * @starget: scsi target struct
1191 *
1192 * Returns 0 if ok. Any other return is assumed to be an error and
1193 * the device is ignored.
1194 */
1195static int
Eric Moored5d135b2009-05-18 13:02:08 -06001196_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001197{
1198 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1199 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1200 struct MPT2SAS_TARGET *sas_target_priv_data;
1201 struct _sas_device *sas_device;
1202 struct _raid_device *raid_device;
1203 unsigned long flags;
1204 struct sas_rphy *rphy;
1205
1206 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1207 if (!sas_target_priv_data)
1208 return -ENOMEM;
1209
1210 starget->hostdata = sas_target_priv_data;
1211 sas_target_priv_data->starget = starget;
1212 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1213
1214 /* RAID volumes */
1215 if (starget->channel == RAID_CHANNEL) {
1216 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1217 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1218 starget->channel);
1219 if (raid_device) {
1220 sas_target_priv_data->handle = raid_device->handle;
1221 sas_target_priv_data->sas_address = raid_device->wwid;
1222 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1223 raid_device->starget = starget;
1224 }
1225 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1226 return 0;
1227 }
1228
1229 /* sas/sata devices */
1230 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1231 rphy = dev_to_rphy(starget->dev.parent);
1232 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1233 rphy->identify.sas_address);
1234
1235 if (sas_device) {
1236 sas_target_priv_data->handle = sas_device->handle;
1237 sas_target_priv_data->sas_address = sas_device->sas_address;
1238 sas_device->starget = starget;
1239 sas_device->id = starget->id;
1240 sas_device->channel = starget->channel;
1241 if (sas_device->hidden_raid_component)
1242 sas_target_priv_data->flags |=
1243 MPT_TARGET_FLAGS_RAID_COMPONENT;
1244 }
1245 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1246
1247 return 0;
1248}
1249
1250/**
Eric Moored5d135b2009-05-18 13:02:08 -06001251 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001252 * @starget: scsi target struct
1253 *
1254 * Returns nothing.
1255 */
1256static void
Eric Moored5d135b2009-05-18 13:02:08 -06001257_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001258{
1259 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1260 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1261 struct MPT2SAS_TARGET *sas_target_priv_data;
1262 struct _sas_device *sas_device;
1263 struct _raid_device *raid_device;
1264 unsigned long flags;
1265 struct sas_rphy *rphy;
1266
1267 sas_target_priv_data = starget->hostdata;
1268 if (!sas_target_priv_data)
1269 return;
1270
1271 if (starget->channel == RAID_CHANNEL) {
1272 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1273 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1274 starget->channel);
1275 if (raid_device) {
1276 raid_device->starget = NULL;
1277 raid_device->sdev = NULL;
1278 }
1279 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1280 goto out;
1281 }
1282
1283 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1284 rphy = dev_to_rphy(starget->dev.parent);
1285 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1286 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001287 if (sas_device && (sas_device->starget == starget) &&
1288 (sas_device->id == starget->id) &&
1289 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001290 sas_device->starget = NULL;
1291
1292 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1293
1294 out:
1295 kfree(sas_target_priv_data);
1296 starget->hostdata = NULL;
1297}
1298
1299/**
Eric Moored5d135b2009-05-18 13:02:08 -06001300 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001301 * @sdev: scsi device struct
1302 *
1303 * Returns 0 if ok. Any other return is assumed to be an error and
1304 * the device is ignored.
1305 */
1306static int
Eric Moored5d135b2009-05-18 13:02:08 -06001307_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001308{
1309 struct Scsi_Host *shost;
1310 struct MPT2SAS_ADAPTER *ioc;
1311 struct MPT2SAS_TARGET *sas_target_priv_data;
1312 struct MPT2SAS_DEVICE *sas_device_priv_data;
1313 struct scsi_target *starget;
1314 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001315 unsigned long flags;
1316
1317 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1318 if (!sas_device_priv_data)
1319 return -ENOMEM;
1320
1321 sas_device_priv_data->lun = sdev->lun;
1322 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1323
1324 starget = scsi_target(sdev);
1325 sas_target_priv_data = starget->hostdata;
1326 sas_target_priv_data->num_luns++;
1327 sas_device_priv_data->sas_target = sas_target_priv_data;
1328 sdev->hostdata = sas_device_priv_data;
1329 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1330 sdev->no_uld_attach = 1;
1331
1332 shost = dev_to_shost(&starget->dev);
1333 ioc = shost_priv(shost);
1334 if (starget->channel == RAID_CHANNEL) {
1335 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1336 raid_device = _scsih_raid_device_find_by_id(ioc,
1337 starget->id, starget->channel);
1338 if (raid_device)
1339 raid_device->sdev = sdev; /* raid is single lun */
1340 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001341 }
1342
Eric Moore635374e2009-03-09 01:21:12 -06001343 return 0;
1344}
1345
1346/**
Eric Moored5d135b2009-05-18 13:02:08 -06001347 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001348 * @sdev: scsi device struct
1349 *
1350 * Returns nothing.
1351 */
1352static void
Eric Moored5d135b2009-05-18 13:02:08 -06001353_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001354{
1355 struct MPT2SAS_TARGET *sas_target_priv_data;
1356 struct scsi_target *starget;
1357
1358 if (!sdev->hostdata)
1359 return;
1360
1361 starget = scsi_target(sdev);
1362 sas_target_priv_data = starget->hostdata;
1363 sas_target_priv_data->num_luns--;
1364 kfree(sdev->hostdata);
1365 sdev->hostdata = NULL;
1366}
1367
1368/**
Eric Moored5d135b2009-05-18 13:02:08 -06001369 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001370 * @ioc: per adapter object
1371 * @sas_device: the sas_device object
1372 * @sdev: scsi device struct
1373 */
1374static void
Eric Moored5d135b2009-05-18 13:02:08 -06001375_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001376 struct _sas_device *sas_device, struct scsi_device *sdev)
1377{
1378 Mpi2ConfigReply_t mpi_reply;
1379 Mpi2SasDevicePage0_t sas_device_pg0;
1380 u32 ioc_status;
1381 u16 flags;
1382 u32 device_info;
1383
1384 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1385 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1386 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1387 ioc->name, __FILE__, __LINE__, __func__);
1388 return;
1389 }
1390
1391 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1392 MPI2_IOCSTATUS_MASK;
1393 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1394 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1395 ioc->name, __FILE__, __LINE__, __func__);
1396 return;
1397 }
1398
1399 flags = le16_to_cpu(sas_device_pg0.Flags);
1400 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1401
1402 sdev_printk(KERN_INFO, sdev,
1403 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1404 "sw_preserve(%s)\n",
1405 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1406 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1407 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1408 "n",
1409 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1410 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1411 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1412}
1413
1414/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301415 * _scsih_is_raid - return boolean indicating device is raid volume
1416 * @dev the device struct object
1417 */
1418static int
1419_scsih_is_raid(struct device *dev)
1420{
1421 struct scsi_device *sdev = to_scsi_device(dev);
1422
1423 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1424}
1425
1426/**
1427 * _scsih_get_resync - get raid volume resync percent complete
1428 * @dev the device struct object
1429 */
1430static void
1431_scsih_get_resync(struct device *dev)
1432{
1433 struct scsi_device *sdev = to_scsi_device(dev);
1434 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1435 static struct _raid_device *raid_device;
1436 unsigned long flags;
1437 Mpi2RaidVolPage0_t vol_pg0;
1438 Mpi2ConfigReply_t mpi_reply;
1439 u32 volume_status_flags;
1440 u8 percent_complete = 0;
1441
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 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1446
1447 if (!raid_device)
1448 goto out;
1449
1450 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1451 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1452 sizeof(Mpi2RaidVolPage0_t))) {
1453 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1454 ioc->name, __FILE__, __LINE__, __func__);
1455 goto out;
1456 }
1457
1458 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1459 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1460 percent_complete = raid_device->percent_complete;
1461 out:
1462 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1463}
1464
1465/**
1466 * _scsih_get_state - get raid volume level
1467 * @dev the device struct object
1468 */
1469static void
1470_scsih_get_state(struct device *dev)
1471{
1472 struct scsi_device *sdev = to_scsi_device(dev);
1473 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1474 static struct _raid_device *raid_device;
1475 unsigned long flags;
1476 Mpi2RaidVolPage0_t vol_pg0;
1477 Mpi2ConfigReply_t mpi_reply;
1478 u32 volstate;
1479 enum raid_state state = RAID_STATE_UNKNOWN;
1480
1481 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1482 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1483 sdev->channel);
1484 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1485
1486 if (!raid_device)
1487 goto out;
1488
1489 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1490 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1491 sizeof(Mpi2RaidVolPage0_t))) {
1492 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1493 ioc->name, __FILE__, __LINE__, __func__);
1494 goto out;
1495 }
1496
1497 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1498 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1499 state = RAID_STATE_RESYNCING;
1500 goto out;
1501 }
1502
1503 switch (vol_pg0.VolumeState) {
1504 case MPI2_RAID_VOL_STATE_OPTIMAL:
1505 case MPI2_RAID_VOL_STATE_ONLINE:
1506 state = RAID_STATE_ACTIVE;
1507 break;
1508 case MPI2_RAID_VOL_STATE_DEGRADED:
1509 state = RAID_STATE_DEGRADED;
1510 break;
1511 case MPI2_RAID_VOL_STATE_FAILED:
1512 case MPI2_RAID_VOL_STATE_MISSING:
1513 state = RAID_STATE_OFFLINE;
1514 break;
1515 }
1516 out:
1517 raid_set_state(mpt2sas_raid_template, dev, state);
1518}
1519
1520/**
1521 * _scsih_set_level - set raid level
1522 * @sdev: scsi device struct
1523 * @raid_device: raid_device object
1524 */
1525static void
1526_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1527{
1528 enum raid_level level = RAID_LEVEL_UNKNOWN;
1529
1530 switch (raid_device->volume_type) {
1531 case MPI2_RAID_VOL_TYPE_RAID0:
1532 level = RAID_LEVEL_0;
1533 break;
1534 case MPI2_RAID_VOL_TYPE_RAID10:
1535 level = RAID_LEVEL_10;
1536 break;
1537 case MPI2_RAID_VOL_TYPE_RAID1E:
1538 level = RAID_LEVEL_1E;
1539 break;
1540 case MPI2_RAID_VOL_TYPE_RAID1:
1541 level = RAID_LEVEL_1;
1542 break;
1543 }
1544
1545 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1546}
1547
1548/**
Eric Moore635374e2009-03-09 01:21:12 -06001549 * _scsih_get_volume_capabilities - volume capabilities
1550 * @ioc: per adapter object
1551 * @sas_device: the raid_device object
1552 */
1553static void
1554_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1555 struct _raid_device *raid_device)
1556{
1557 Mpi2RaidVolPage0_t *vol_pg0;
1558 Mpi2RaidPhysDiskPage0_t pd_pg0;
1559 Mpi2SasDevicePage0_t sas_device_pg0;
1560 Mpi2ConfigReply_t mpi_reply;
1561 u16 sz;
1562 u8 num_pds;
1563
1564 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1565 &num_pds)) || !num_pds) {
1566 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1567 ioc->name, __FILE__, __LINE__, __func__);
1568 return;
1569 }
1570
1571 raid_device->num_pds = num_pds;
1572 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1573 sizeof(Mpi2RaidVol0PhysDisk_t));
1574 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1575 if (!vol_pg0) {
1576 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1577 ioc->name, __FILE__, __LINE__, __func__);
1578 return;
1579 }
1580
1581 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1582 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1583 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1584 ioc->name, __FILE__, __LINE__, __func__);
1585 kfree(vol_pg0);
1586 return;
1587 }
1588
1589 raid_device->volume_type = vol_pg0->VolumeType;
1590
1591 /* figure out what the underlying devices are by
1592 * obtaining the device_info bits for the 1st device
1593 */
1594 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1595 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1596 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1597 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1598 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1599 le16_to_cpu(pd_pg0.DevHandle)))) {
1600 raid_device->device_info =
1601 le32_to_cpu(sas_device_pg0.DeviceInfo);
1602 }
1603 }
1604
1605 kfree(vol_pg0);
1606}
1607
1608/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301609 * _scsih_enable_tlr - setting TLR flags
1610 * @ioc: per adapter object
1611 * @sdev: scsi device struct
1612 *
1613 * Enabling Transaction Layer Retries for tape devices when
1614 * vpd page 0x90 is present
1615 *
1616 */
1617static void
1618_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1619{
1620 /* only for TAPE */
1621 if (sdev->type != TYPE_TAPE)
1622 return;
1623
1624 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1625 return;
1626
1627 sas_enable_tlr(sdev);
1628 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1629 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1630 return;
1631
1632}
1633
1634/**
Eric Moored5d135b2009-05-18 13:02:08 -06001635 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001636 * @sdev: scsi device struct
1637 *
1638 * Returns 0 if ok. Any other return is assumed to be an error and
1639 * the device is ignored.
1640 */
1641static int
Eric Moored5d135b2009-05-18 13:02:08 -06001642_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001643{
1644 struct Scsi_Host *shost = sdev->host;
1645 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1646 struct MPT2SAS_DEVICE *sas_device_priv_data;
1647 struct MPT2SAS_TARGET *sas_target_priv_data;
1648 struct _sas_device *sas_device;
1649 struct _raid_device *raid_device;
1650 unsigned long flags;
1651 int qdepth;
1652 u8 ssp_target = 0;
1653 char *ds = "";
1654 char *r_level = "";
1655
1656 qdepth = 1;
1657 sas_device_priv_data = sdev->hostdata;
1658 sas_device_priv_data->configured_lun = 1;
1659 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1660 sas_target_priv_data = sas_device_priv_data->sas_target;
1661
1662 /* raid volume handling */
1663 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1664
1665 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1666 raid_device = _scsih_raid_device_find_by_handle(ioc,
1667 sas_target_priv_data->handle);
1668 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1669 if (!raid_device) {
1670 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1671 ioc->name, __FILE__, __LINE__, __func__);
1672 return 0;
1673 }
1674
1675 _scsih_get_volume_capabilities(ioc, raid_device);
1676
1677 /* RAID Queue Depth Support
1678 * IS volume = underlying qdepth of drive type, either
1679 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1680 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1681 */
1682 if (raid_device->device_info &
1683 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1684 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1685 ds = "SSP";
1686 } else {
1687 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1688 if (raid_device->device_info &
1689 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1690 ds = "SATA";
1691 else
1692 ds = "STP";
1693 }
1694
1695 switch (raid_device->volume_type) {
1696 case MPI2_RAID_VOL_TYPE_RAID0:
1697 r_level = "RAID0";
1698 break;
1699 case MPI2_RAID_VOL_TYPE_RAID1E:
1700 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301701 if (ioc->manu_pg10.OEMIdentifier &&
1702 (ioc->manu_pg10.GenericFlags0 &
1703 MFG10_GF0_R10_DISPLAY) &&
1704 !(raid_device->num_pds % 2))
1705 r_level = "RAID10";
1706 else
1707 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001708 break;
1709 case MPI2_RAID_VOL_TYPE_RAID1:
1710 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1711 r_level = "RAID1";
1712 break;
1713 case MPI2_RAID_VOL_TYPE_RAID10:
1714 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1715 r_level = "RAID10";
1716 break;
1717 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1718 default:
1719 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1720 r_level = "RAIDX";
1721 break;
1722 }
1723
1724 sdev_printk(KERN_INFO, sdev, "%s: "
1725 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1726 r_level, raid_device->handle,
1727 (unsigned long long)raid_device->wwid,
1728 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07001729 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301730 /* raid transport support */
1731 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06001732 return 0;
1733 }
1734
1735 /* non-raid handling */
1736 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1737 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1738 sas_device_priv_data->sas_target->sas_address);
1739 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1740 if (sas_device) {
1741 if (sas_target_priv_data->flags &
1742 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1743 mpt2sas_config_get_volume_handle(ioc,
1744 sas_device->handle, &sas_device->volume_handle);
1745 mpt2sas_config_get_volume_wwid(ioc,
1746 sas_device->volume_handle,
1747 &sas_device->volume_wwid);
1748 }
1749 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1750 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1751 ssp_target = 1;
1752 ds = "SSP";
1753 } else {
1754 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1755 if (sas_device->device_info &
1756 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1757 ds = "STP";
1758 else if (sas_device->device_info &
1759 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1760 ds = "SATA";
1761 }
1762
1763 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1764 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1765 ds, sas_device->handle,
1766 (unsigned long long)sas_device->sas_address,
1767 (unsigned long long)sas_device->device_name);
1768 sdev_printk(KERN_INFO, sdev, "%s: "
1769 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1770 (unsigned long long) sas_device->enclosure_logical_id,
1771 sas_device->slot);
1772
1773 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001774 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001775 }
1776
Mike Christiee881a172009-10-15 17:46:39 -07001777 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06001778
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301779 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06001780 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301781 _scsih_enable_tlr(ioc, sdev);
1782 }
Eric Moore635374e2009-03-09 01:21:12 -06001783 return 0;
1784}
1785
1786/**
Eric Moored5d135b2009-05-18 13:02:08 -06001787 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001788 * @sdev: scsi device struct
1789 * @bdev: pointer to block device context
1790 * @capacity: device size (in 512 byte sectors)
1791 * @params: three element array to place output:
1792 * params[0] number of heads (max 255)
1793 * params[1] number of sectors (max 63)
1794 * params[2] number of cylinders
1795 *
1796 * Return nothing.
1797 */
1798static int
Eric Moored5d135b2009-05-18 13:02:08 -06001799_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001800 sector_t capacity, int params[])
1801{
1802 int heads;
1803 int sectors;
1804 sector_t cylinders;
1805 ulong dummy;
1806
1807 heads = 64;
1808 sectors = 32;
1809
1810 dummy = heads * sectors;
1811 cylinders = capacity;
1812 sector_div(cylinders, dummy);
1813
1814 /*
1815 * Handle extended translation size for logical drives
1816 * > 1Gb
1817 */
1818 if ((ulong)capacity >= 0x200000) {
1819 heads = 255;
1820 sectors = 63;
1821 dummy = heads * sectors;
1822 cylinders = capacity;
1823 sector_div(cylinders, dummy);
1824 }
1825
1826 /* return result */
1827 params[0] = heads;
1828 params[1] = sectors;
1829 params[2] = cylinders;
1830
1831 return 0;
1832}
1833
1834/**
1835 * _scsih_response_code - translation of device response code
1836 * @ioc: per adapter object
1837 * @response_code: response code returned by the device
1838 *
1839 * Return nothing.
1840 */
1841static void
1842_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1843{
1844 char *desc;
1845
1846 switch (response_code) {
1847 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1848 desc = "task management request completed";
1849 break;
1850 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1851 desc = "invalid frame";
1852 break;
1853 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1854 desc = "task management request not supported";
1855 break;
1856 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1857 desc = "task management request failed";
1858 break;
1859 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1860 desc = "task management request succeeded";
1861 break;
1862 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1863 desc = "invalid lun";
1864 break;
1865 case 0xA:
1866 desc = "overlapped tag attempted";
1867 break;
1868 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1869 desc = "task queued, however not sent to target";
1870 break;
1871 default:
1872 desc = "unknown";
1873 break;
1874 }
1875 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1876 ioc->name, response_code, desc);
1877}
1878
1879/**
Eric Moored5d135b2009-05-18 13:02:08 -06001880 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001881 * @ioc: per adapter object
1882 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301883 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001884 * @reply: reply message frame(lower 32bit addr)
1885 * Context: none.
1886 *
1887 * The callback handler when using scsih_issue_tm.
1888 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301889 * Return 1 meaning mf should be freed from _base_interrupt
1890 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06001891 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301892static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301893_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001894{
1895 MPI2DefaultReply_t *mpi_reply;
1896
1897 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301898 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001899 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301900 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001901 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1902 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1903 if (mpi_reply) {
1904 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1905 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1906 }
1907 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1908 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301909 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001910}
1911
1912/**
1913 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1914 * @ioc: per adapter object
1915 * @handle: device handle
1916 *
1917 * During taskmangement request, we need to freeze the device queue.
1918 */
1919void
1920mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1921{
1922 struct MPT2SAS_DEVICE *sas_device_priv_data;
1923 struct scsi_device *sdev;
1924 u8 skip = 0;
1925
1926 shost_for_each_device(sdev, ioc->shost) {
1927 if (skip)
1928 continue;
1929 sas_device_priv_data = sdev->hostdata;
1930 if (!sas_device_priv_data)
1931 continue;
1932 if (sas_device_priv_data->sas_target->handle == handle) {
1933 sas_device_priv_data->sas_target->tm_busy = 1;
1934 skip = 1;
1935 ioc->ignore_loginfos = 1;
1936 }
1937 }
1938}
1939
1940/**
1941 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1942 * @ioc: per adapter object
1943 * @handle: device handle
1944 *
1945 * During taskmangement request, we need to freeze the device queue.
1946 */
1947void
1948mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1949{
1950 struct MPT2SAS_DEVICE *sas_device_priv_data;
1951 struct scsi_device *sdev;
1952 u8 skip = 0;
1953
1954 shost_for_each_device(sdev, ioc->shost) {
1955 if (skip)
1956 continue;
1957 sas_device_priv_data = sdev->hostdata;
1958 if (!sas_device_priv_data)
1959 continue;
1960 if (sas_device_priv_data->sas_target->handle == handle) {
1961 sas_device_priv_data->sas_target->tm_busy = 0;
1962 skip = 1;
1963 ioc->ignore_loginfos = 0;
1964 }
1965 }
1966}
1967
1968/**
1969 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1970 * @ioc: per adapter struct
1971 * @device_handle: device handle
1972 * @lun: lun number
1973 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1974 * @smid_task: smid assigned to the task
1975 * @timeout: timeout in seconds
1976 * Context: The calling function needs to acquire the tm_cmds.mutex
1977 *
1978 * A generic API for sending task management requests to firmware.
1979 *
1980 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1981 * this API.
1982 *
1983 * The callback index is set inside `ioc->tm_cb_idx`.
1984 *
1985 * Return nothing.
1986 */
1987void
1988mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1989 u8 type, u16 smid_task, ulong timeout)
1990{
1991 Mpi2SCSITaskManagementRequest_t *mpi_request;
1992 Mpi2SCSITaskManagementReply_t *mpi_reply;
1993 u16 smid = 0;
1994 u32 ioc_state;
1995 unsigned long timeleft;
Eric Moore635374e2009-03-09 01:21:12 -06001996
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301997 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1998 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1999 __func__, ioc->name);
2000 return;
2001 }
2002
2003 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002004 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2005 __func__, ioc->name);
2006 return;
2007 }
Eric Moore635374e2009-03-09 01:21:12 -06002008
2009 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2010 if (ioc_state & MPI2_DOORBELL_USED) {
2011 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
2012 "active!\n", ioc->name));
2013 goto issue_host_reset;
2014 }
2015
2016 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2017 mpt2sas_base_fault_info(ioc, ioc_state &
2018 MPI2_DOORBELL_DATA_MASK);
2019 goto issue_host_reset;
2020 }
2021
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302022 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002023 if (!smid) {
2024 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2025 ioc->name, __func__);
2026 return;
2027 }
2028
2029 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302030 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2031 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002032 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2033 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2034 ioc->tm_cmds.smid = smid;
2035 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2036 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2037 mpi_request->DevHandle = cpu_to_le16(handle);
2038 mpi_request->TaskType = type;
2039 mpi_request->TaskMID = cpu_to_le16(smid_task);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302040 mpi_request->VP_ID = 0; /* TODO */
2041 mpi_request->VF_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002042 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2043 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302044 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302045 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002046 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2047 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2048 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2049 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2050 ioc->name, __func__);
2051 _debug_dump_mf(mpi_request,
2052 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2053 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
2054 goto issue_host_reset;
2055 }
2056
2057 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2058 mpi_reply = ioc->tm_cmds.reply;
2059 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2060 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2061 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2062 le32_to_cpu(mpi_reply->IOCLogInfo),
2063 le32_to_cpu(mpi_reply->TerminationCount)));
2064 if (ioc->logging_level & MPT_DEBUG_TM)
2065 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2066 }
2067 return;
2068 issue_host_reset:
2069 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
2070}
2071
2072/**
Eric Moored5d135b2009-05-18 13:02:08 -06002073 * _scsih_abort - eh threads main abort routine
Eric Moore635374e2009-03-09 01:21:12 -06002074 * @sdev: scsi device struct
2075 *
2076 * Returns SUCCESS if command aborted else FAILED
2077 */
2078static int
Eric Moored5d135b2009-05-18 13:02:08 -06002079_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002080{
2081 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2082 struct MPT2SAS_DEVICE *sas_device_priv_data;
2083 u16 smid;
2084 u16 handle;
2085 int r;
2086 struct scsi_cmnd *scmd_lookup;
2087
2088 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
2089 ioc->name, scmd);
2090 scsi_print_command(scmd);
2091
2092 sas_device_priv_data = scmd->device->hostdata;
2093 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2094 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2095 ioc->name, scmd);
2096 scmd->result = DID_NO_CONNECT << 16;
2097 scmd->scsi_done(scmd);
2098 r = SUCCESS;
2099 goto out;
2100 }
2101
2102 /* search for the command */
2103 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2104 if (!smid) {
2105 scmd->result = DID_RESET << 16;
2106 r = SUCCESS;
2107 goto out;
2108 }
2109
2110 /* for hidden raid components and volumes this is not supported */
2111 if (sas_device_priv_data->sas_target->flags &
2112 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2113 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2114 scmd->result = DID_RESET << 16;
2115 r = FAILED;
2116 goto out;
2117 }
2118
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302119 mpt2sas_halt_firmware(ioc);
2120
Eric Moore635374e2009-03-09 01:21:12 -06002121 mutex_lock(&ioc->tm_cmds.mutex);
2122 handle = sas_device_priv_data->sas_target->handle;
2123 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
2124 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
2125
2126 /* sanity check - see whether command actually completed */
2127 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
2128 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
2129 r = FAILED;
2130 else
2131 r = SUCCESS;
2132 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2133 mutex_unlock(&ioc->tm_cmds.mutex);
2134
2135 out:
2136 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
2137 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2138 return r;
2139}
2140
Eric Moore635374e2009-03-09 01:21:12 -06002141/**
Eric Moored5d135b2009-05-18 13:02:08 -06002142 * _scsih_dev_reset - eh threads main device reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002143 * @sdev: scsi device struct
2144 *
2145 * Returns SUCCESS if command aborted else FAILED
2146 */
2147static int
Eric Moored5d135b2009-05-18 13:02:08 -06002148_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002149{
2150 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2151 struct MPT2SAS_DEVICE *sas_device_priv_data;
2152 struct _sas_device *sas_device;
2153 unsigned long flags;
2154 u16 handle;
2155 int r;
2156
Eric Moore993e0da2009-05-18 13:00:45 -06002157 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002158 ioc->name, scmd);
2159 scsi_print_command(scmd);
2160
2161 sas_device_priv_data = scmd->device->hostdata;
2162 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2163 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2164 ioc->name, scmd);
2165 scmd->result = DID_NO_CONNECT << 16;
2166 scmd->scsi_done(scmd);
2167 r = SUCCESS;
2168 goto out;
2169 }
2170
2171 /* for hidden raid components obtain the volume_handle */
2172 handle = 0;
2173 if (sas_device_priv_data->sas_target->flags &
2174 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2175 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2176 sas_device = _scsih_sas_device_find_by_handle(ioc,
2177 sas_device_priv_data->sas_target->handle);
2178 if (sas_device)
2179 handle = sas_device->volume_handle;
2180 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2181 } else
2182 handle = sas_device_priv_data->sas_target->handle;
2183
2184 if (!handle) {
2185 scmd->result = DID_RESET << 16;
2186 r = FAILED;
2187 goto out;
2188 }
2189
2190 mutex_lock(&ioc->tm_cmds.mutex);
2191 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore993e0da2009-05-18 13:00:45 -06002192 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
2193 30);
2194
2195 /*
2196 * sanity check see whether all commands to this device been
2197 * completed
2198 */
2199 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
2200 scmd->device->lun, scmd->device->channel))
2201 r = FAILED;
2202 else
2203 r = SUCCESS;
2204 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2205 mutex_unlock(&ioc->tm_cmds.mutex);
2206
2207 out:
2208 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2209 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2210 return r;
2211}
2212
2213/**
Eric Moored5d135b2009-05-18 13:02:08 -06002214 * _scsih_target_reset - eh threads main target reset routine
Eric Moore993e0da2009-05-18 13:00:45 -06002215 * @sdev: scsi device struct
2216 *
2217 * Returns SUCCESS if command aborted else FAILED
2218 */
2219static int
Eric Moored5d135b2009-05-18 13:02:08 -06002220_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002221{
2222 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2223 struct MPT2SAS_DEVICE *sas_device_priv_data;
2224 struct _sas_device *sas_device;
2225 unsigned long flags;
2226 u16 handle;
2227 int r;
2228
2229 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2230 ioc->name, scmd);
2231 scsi_print_command(scmd);
2232
2233 sas_device_priv_data = scmd->device->hostdata;
2234 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2235 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2236 ioc->name, scmd);
2237 scmd->result = DID_NO_CONNECT << 16;
2238 scmd->scsi_done(scmd);
2239 r = SUCCESS;
2240 goto out;
2241 }
2242
2243 /* for hidden raid components obtain the volume_handle */
2244 handle = 0;
2245 if (sas_device_priv_data->sas_target->flags &
2246 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2247 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2248 sas_device = _scsih_sas_device_find_by_handle(ioc,
2249 sas_device_priv_data->sas_target->handle);
2250 if (sas_device)
2251 handle = sas_device->volume_handle;
2252 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2253 } else
2254 handle = sas_device_priv_data->sas_target->handle;
2255
2256 if (!handle) {
2257 scmd->result = DID_RESET << 16;
2258 r = FAILED;
2259 goto out;
2260 }
2261
2262 mutex_lock(&ioc->tm_cmds.mutex);
2263 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore635374e2009-03-09 01:21:12 -06002264 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2265
2266 /*
2267 * sanity check see whether all commands to this target been
2268 * completed
2269 */
2270 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2271 scmd->device->channel))
2272 r = FAILED;
2273 else
2274 r = SUCCESS;
2275 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2276 mutex_unlock(&ioc->tm_cmds.mutex);
2277
2278 out:
2279 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2280 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2281 return r;
2282}
2283
2284/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302285 * _scsih_host_reset - eh threads main host reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002286 * @sdev: scsi device struct
2287 *
2288 * Returns SUCCESS if command aborted else FAILED
2289 */
2290static int
Eric Moored5d135b2009-05-18 13:02:08 -06002291_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002292{
2293 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2294 int r, retval;
2295
2296 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2297 ioc->name, scmd);
2298 scsi_print_command(scmd);
2299
2300 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2301 FORCE_BIG_HAMMER);
2302 r = (retval < 0) ? FAILED : SUCCESS;
2303 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2304 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2305
2306 return r;
2307}
2308
2309/**
2310 * _scsih_fw_event_add - insert and queue up fw_event
2311 * @ioc: per adapter object
2312 * @fw_event: object describing the event
2313 * Context: This function will acquire ioc->fw_event_lock.
2314 *
2315 * This adds the firmware event object into link list, then queues it up to
2316 * be processed from user context.
2317 *
2318 * Return nothing.
2319 */
2320static void
2321_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2322{
2323 unsigned long flags;
2324
2325 if (ioc->firmware_event_thread == NULL)
2326 return;
2327
2328 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2329 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302330 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2331 queue_delayed_work(ioc->firmware_event_thread,
2332 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002333 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2334}
2335
2336/**
2337 * _scsih_fw_event_free - delete fw_event
2338 * @ioc: per adapter object
2339 * @fw_event: object describing the event
2340 * Context: This function will acquire ioc->fw_event_lock.
2341 *
2342 * This removes firmware event object from link list, frees associated memory.
2343 *
2344 * Return nothing.
2345 */
2346static void
2347_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2348 *fw_event)
2349{
2350 unsigned long flags;
2351
2352 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2353 list_del(&fw_event->list);
2354 kfree(fw_event->event_data);
2355 kfree(fw_event);
2356 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2357}
2358
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302359
Eric Moore635374e2009-03-09 01:21:12 -06002360/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302361 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002362 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002363 *
2364 * Return nothing.
2365 */
2366static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302367_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002368{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302369 struct fw_event_work *fw_event;
2370
2371 if (ioc->wait_for_port_enable_to_complete)
2372 return;
2373 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2374 if (!fw_event)
2375 return;
2376 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2377 fw_event->ioc = ioc;
2378 _scsih_fw_event_add(ioc, fw_event);
2379}
2380
2381/**
2382 * _scsih_fw_event_cleanup_queue - cleanup event queue
2383 * @ioc: per adapter object
2384 *
2385 * Walk the firmware event queue, either killing timers, or waiting
2386 * for outstanding events to complete
2387 *
2388 * Return nothing.
2389 */
2390static void
2391_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2392{
2393 struct fw_event_work *fw_event, *next;
2394
2395 if (list_empty(&ioc->fw_event_list) ||
2396 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002397 return;
2398
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302399 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2400 if (cancel_delayed_work(&fw_event->delayed_work)) {
2401 _scsih_fw_event_free(ioc, fw_event);
2402 continue;
2403 }
2404 fw_event->cancel_pending_work = 1;
2405 }
Eric Moore635374e2009-03-09 01:21:12 -06002406}
2407
Eric Moore635374e2009-03-09 01:21:12 -06002408
2409/**
2410 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2411 * @ioc: per adapter object
2412 * @handle: device handle
2413 *
2414 * During device pull we need to appropiately set the sdev state.
2415 */
2416static void
2417_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2418{
2419 struct MPT2SAS_DEVICE *sas_device_priv_data;
2420 struct scsi_device *sdev;
2421
2422 shost_for_each_device(sdev, ioc->shost) {
2423 sas_device_priv_data = sdev->hostdata;
2424 if (!sas_device_priv_data)
2425 continue;
2426 if (!sas_device_priv_data->block)
2427 continue;
2428 if (sas_device_priv_data->sas_target->handle == handle) {
2429 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2430 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2431 "handle(0x%04x)\n", ioc->name, handle));
2432 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302433 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002434 }
2435 }
2436}
2437
2438/**
2439 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2440 * @ioc: per adapter object
2441 * @handle: device handle
2442 *
2443 * During device pull we need to appropiately set the sdev state.
2444 */
2445static void
2446_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2447{
2448 struct MPT2SAS_DEVICE *sas_device_priv_data;
2449 struct scsi_device *sdev;
2450
2451 shost_for_each_device(sdev, ioc->shost) {
2452 sas_device_priv_data = sdev->hostdata;
2453 if (!sas_device_priv_data)
2454 continue;
2455 if (sas_device_priv_data->block)
2456 continue;
2457 if (sas_device_priv_data->sas_target->handle == handle) {
2458 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2459 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2460 "handle(0x%04x)\n", ioc->name, handle));
2461 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302462 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002463 }
2464 }
2465}
2466
2467/**
2468 * _scsih_block_io_to_children_attached_to_ex
2469 * @ioc: per adapter object
2470 * @sas_expander: the sas_device object
2471 *
2472 * This routine set sdev state to SDEV_BLOCK for all devices
2473 * attached to this expander. This function called when expander is
2474 * pulled.
2475 */
2476static void
2477_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2478 struct _sas_node *sas_expander)
2479{
2480 struct _sas_port *mpt2sas_port;
2481 struct _sas_device *sas_device;
2482 struct _sas_node *expander_sibling;
2483 unsigned long flags;
2484
2485 if (!sas_expander)
2486 return;
2487
2488 list_for_each_entry(mpt2sas_port,
2489 &sas_expander->sas_port_list, port_list) {
2490 if (mpt2sas_port->remote_identify.device_type ==
2491 SAS_END_DEVICE) {
2492 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2493 sas_device =
2494 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2495 mpt2sas_port->remote_identify.sas_address);
2496 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2497 if (!sas_device)
2498 continue;
2499 _scsih_block_io_device(ioc, sas_device->handle);
2500 }
2501 }
2502
2503 list_for_each_entry(mpt2sas_port,
2504 &sas_expander->sas_port_list, port_list) {
2505
2506 if (mpt2sas_port->remote_identify.device_type ==
2507 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2508 mpt2sas_port->remote_identify.device_type ==
2509 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2510
2511 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2512 expander_sibling =
2513 mpt2sas_scsih_expander_find_by_sas_address(
2514 ioc, mpt2sas_port->remote_identify.sas_address);
2515 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2516 _scsih_block_io_to_children_attached_to_ex(ioc,
2517 expander_sibling);
2518 }
2519 }
2520}
2521
2522/**
2523 * _scsih_block_io_to_children_attached_directly
2524 * @ioc: per adapter object
2525 * @event_data: topology change event data
2526 *
2527 * This routine set sdev state to SDEV_BLOCK for all devices
2528 * direct attached during device pull.
2529 */
2530static void
2531_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2532 Mpi2EventDataSasTopologyChangeList_t *event_data)
2533{
2534 int i;
2535 u16 handle;
2536 u16 reason_code;
2537 u8 phy_number;
2538
2539 for (i = 0; i < event_data->NumEntries; i++) {
2540 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2541 if (!handle)
2542 continue;
2543 phy_number = event_data->StartPhyNum + i;
2544 reason_code = event_data->PHY[i].PhyStatus &
2545 MPI2_EVENT_SAS_TOPO_RC_MASK;
2546 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2547 _scsih_block_io_device(ioc, handle);
2548 }
2549}
2550
2551/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302552 * _scsih_tm_tr_send - send task management request
2553 * @ioc: per adapter object
2554 * @handle: device handle
2555 * Context: interrupt time.
2556 *
2557 * This code is to initiate the device removal handshake protocal
2558 * with controller firmware. This function will issue target reset
2559 * using high priority request queue. It will send a sas iounit
2560 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2561 *
2562 * This is designed to send muliple task management request at the same
2563 * time to the fifo. If the fifo is full, we will append the request,
2564 * and process it in a future completion.
2565 */
2566static void
2567_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2568{
2569 Mpi2SCSITaskManagementRequest_t *mpi_request;
2570 struct MPT2SAS_TARGET *sas_target_priv_data;
2571 u16 smid;
2572 struct _sas_device *sas_device;
2573 unsigned long flags;
2574 struct _tr_list *delayed_tr;
2575
2576 if (ioc->shost_recovery) {
2577 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2578 __func__, ioc->name);
2579 return;
2580 }
2581
2582 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2583 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302584 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2585
2586 /* skip is hidden raid component */
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302587 if (sas_device && sas_device->hidden_raid_component)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302588 return;
2589
2590 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2591 if (!smid) {
2592 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2593 if (!delayed_tr)
2594 return;
2595 INIT_LIST_HEAD(&delayed_tr->list);
2596 delayed_tr->handle = handle;
2597 delayed_tr->state = MPT2SAS_REQ_SAS_CNTRL;
2598 list_add_tail(&delayed_tr->list,
2599 &ioc->delayed_tr_list);
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302600 if (sas_device && sas_device->starget) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302601 dewtprintk(ioc, starget_printk(KERN_INFO,
2602 sas_device->starget, "DELAYED:tr:handle(0x%04x), "
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302603 "(open)\n", handle));
2604 } else {
2605 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2606 "DELAYED:tr:handle(0x%04x), (open)\n",
2607 ioc->name, handle));
2608 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302609 return;
2610 }
2611
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302612 if (sas_device) {
2613 sas_device->state |= MPTSAS_STATE_TR_SEND;
2614 sas_device->state |= MPT2SAS_REQ_SAS_CNTRL;
2615 if (sas_device->starget && sas_device->starget->hostdata) {
2616 sas_target_priv_data = sas_device->starget->hostdata;
2617 sas_target_priv_data->tm_busy = 1;
2618 dewtprintk(ioc, starget_printk(KERN_INFO,
2619 sas_device->starget, "tr:handle(0x%04x), (open)\n",
2620 handle));
2621 }
2622 } else {
2623 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2624 "tr:handle(0x%04x), (open)\n", ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302625 }
2626
2627 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2628 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2629 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2630 mpi_request->DevHandle = cpu_to_le16(handle);
2631 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302632 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2633}
2634
2635
2636
2637/**
2638 * _scsih_sas_control_complete - completion routine
2639 * @ioc: per adapter object
2640 * @smid: system request message index
2641 * @msix_index: MSIX table index supplied by the OS
2642 * @reply: reply message frame(lower 32bit addr)
2643 * Context: interrupt time.
2644 *
2645 * This is the sas iounit controll completion routine.
2646 * This code is part of the code to initiate the device removal
2647 * handshake protocal with controller firmware.
2648 *
2649 * Return 1 meaning mf should be freed from _base_interrupt
2650 * 0 means the mf is freed from this function.
2651 */
2652static u8
2653_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2654 u8 msix_index, u32 reply)
2655{
2656 unsigned long flags;
2657 u16 handle;
2658 struct _sas_device *sas_device;
2659 Mpi2SasIoUnitControlReply_t *mpi_reply =
2660 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2661
2662 handle = le16_to_cpu(mpi_reply->DevHandle);
2663
2664 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2665 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302666 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2667
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302668 if (sas_device) {
2669 sas_device->state |= MPTSAS_STATE_CNTRL_COMPLETE;
2670 if (sas_device->starget)
2671 dewtprintk(ioc, starget_printk(KERN_INFO,
2672 sas_device->starget,
2673 "sc_complete:handle(0x%04x), "
2674 "ioc_status(0x%04x), loginfo(0x%08x)\n",
2675 handle, le16_to_cpu(mpi_reply->IOCStatus),
2676 le32_to_cpu(mpi_reply->IOCLogInfo)));
2677 } else {
2678 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302679 "sc_complete:handle(0x%04x), "
2680 "ioc_status(0x%04x), loginfo(0x%08x)\n",
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302681 ioc->name, handle, le16_to_cpu(mpi_reply->IOCStatus),
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302682 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302683 }
2684
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302685 return 1;
2686}
2687
2688/**
2689 * _scsih_tm_tr_complete -
2690 * @ioc: per adapter object
2691 * @smid: system request message index
2692 * @msix_index: MSIX table index supplied by the OS
2693 * @reply: reply message frame(lower 32bit addr)
2694 * Context: interrupt time.
2695 *
2696 * This is the target reset completion routine.
2697 * This code is part of the code to initiate the device removal
2698 * handshake protocal with controller firmware.
2699 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2700 *
2701 * Return 1 meaning mf should be freed from _base_interrupt
2702 * 0 means the mf is freed from this function.
2703 */
2704static u8
2705_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2706 u32 reply)
2707{
2708 unsigned long flags;
2709 u16 handle;
2710 struct _sas_device *sas_device;
2711 Mpi2SCSITaskManagementReply_t *mpi_reply =
2712 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2713 Mpi2SasIoUnitControlRequest_t *mpi_request;
2714 u16 smid_sas_ctrl;
2715 struct MPT2SAS_TARGET *sas_target_priv_data;
2716 struct _tr_list *delayed_tr;
2717 u8 rc;
2718
2719 handle = le16_to_cpu(mpi_reply->DevHandle);
2720 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2721 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302722 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2723
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302724 if (sas_device) {
2725 sas_device->state |= MPTSAS_STATE_TR_COMPLETE;
2726 if (sas_device->starget) {
2727 dewtprintk(ioc, starget_printk(KERN_INFO,
2728 sas_device->starget, "tr_complete:handle(0x%04x), "
2729 "(%s) ioc_status(0x%04x), loginfo(0x%08x), "
2730 "completed(%d)\n", sas_device->handle,
2731 (sas_device->state & MPT2SAS_REQ_SAS_CNTRL) ?
2732 "open" : "active",
2733 le16_to_cpu(mpi_reply->IOCStatus),
2734 le32_to_cpu(mpi_reply->IOCLogInfo),
2735 le32_to_cpu(mpi_reply->TerminationCount)));
2736 if (sas_device->starget->hostdata) {
2737 sas_target_priv_data =
2738 sas_device->starget->hostdata;
2739 sas_target_priv_data->tm_busy = 0;
2740 }
2741 }
2742 } else {
2743 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2744 "tr_complete:handle(0x%04x), (open) ioc_status(0x%04x), "
2745 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2746 handle, le16_to_cpu(mpi_reply->IOCStatus),
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302747 le32_to_cpu(mpi_reply->IOCLogInfo),
2748 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302749 }
2750
2751 if (!list_empty(&ioc->delayed_tr_list)) {
2752 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2753 struct _tr_list, list);
2754 mpt2sas_base_free_smid(ioc, smid);
2755 if (delayed_tr->state & MPT2SAS_REQ_SAS_CNTRL)
2756 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2757 list_del(&delayed_tr->list);
2758 kfree(delayed_tr);
2759 rc = 0; /* tells base_interrupt not to free mf */
2760 } else
2761 rc = 1;
2762
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302763 if (sas_device && !(sas_device->state & MPT2SAS_REQ_SAS_CNTRL))
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302764 return rc;
2765
2766 if (ioc->shost_recovery) {
2767 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2768 __func__, ioc->name);
2769 return rc;
2770 }
2771
2772 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2773 if (!smid_sas_ctrl) {
2774 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2775 ioc->name, __func__);
2776 return rc;
2777 }
2778
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302779 if (sas_device)
2780 sas_device->state |= MPTSAS_STATE_CNTRL_SEND;
2781
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302782 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2783 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2784 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2785 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2786 mpi_request->DevHandle = mpi_reply->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302787 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2788 return rc;
2789}
2790
2791/**
Eric Moore635374e2009-03-09 01:21:12 -06002792 * _scsih_check_topo_delete_events - sanity check on topo events
2793 * @ioc: per adapter object
2794 * @event_data: the event data payload
2795 *
2796 * This routine added to better handle cable breaker.
2797 *
2798 * This handles the case where driver recieves multiple expander
2799 * add and delete events in a single shot. When there is a delete event
2800 * the routine will void any pending add events waiting in the event queue.
2801 *
2802 * Return nothing.
2803 */
2804static void
2805_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2806 Mpi2EventDataSasTopologyChangeList_t *event_data)
2807{
2808 struct fw_event_work *fw_event;
2809 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2810 u16 expander_handle;
2811 struct _sas_node *sas_expander;
2812 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302813 int i, reason_code;
2814 u16 handle;
2815
2816 for (i = 0 ; i < event_data->NumEntries; i++) {
2817 if (event_data->PHY[i].PhyStatus &
2818 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2819 continue;
2820 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2821 if (!handle)
2822 continue;
2823 reason_code = event_data->PHY[i].PhyStatus &
2824 MPI2_EVENT_SAS_TOPO_RC_MASK;
2825 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2826 _scsih_tm_tr_send(ioc, handle);
2827 }
Eric Moore635374e2009-03-09 01:21:12 -06002828
2829 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2830 if (expander_handle < ioc->sas_hba.num_phys) {
2831 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2832 return;
2833 }
2834
2835 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2836 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2837 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2838 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2839 expander_handle);
2840 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2841 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2842 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2843 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2844
2845 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2846 return;
2847
2848 /* mark ignore flag for pending events */
2849 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2850 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2851 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2852 fw_event->ignore)
2853 continue;
2854 local_event_data = fw_event->event_data;
2855 if (local_event_data->ExpStatus ==
2856 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2857 local_event_data->ExpStatus ==
2858 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2859 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2860 expander_handle) {
2861 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2862 "setting ignoring flag\n", ioc->name));
2863 fw_event->ignore = 1;
2864 }
2865 }
2866 }
2867 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2868}
2869
2870/**
Eric Moore635374e2009-03-09 01:21:12 -06002871 * _scsih_flush_running_cmds - completing outstanding commands.
2872 * @ioc: per adapter object
2873 *
2874 * The flushing out of all pending scmd commands following host reset,
2875 * where all IO is dropped to the floor.
2876 *
2877 * Return nothing.
2878 */
2879static void
2880_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2881{
2882 struct scsi_cmnd *scmd;
2883 u16 smid;
2884 u16 count = 0;
2885
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302886 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
2887 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002888 if (!scmd)
2889 continue;
2890 count++;
2891 mpt2sas_base_free_smid(ioc, smid);
2892 scsi_dma_unmap(scmd);
2893 scmd->result = DID_RESET << 16;
2894 scmd->scsi_done(scmd);
2895 }
2896 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2897 ioc->name, count));
2898}
2899
2900/**
Eric Moore3c621b32009-05-18 12:59:41 -06002901 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2902 * @scmd: pointer to scsi command object
2903 * @mpi_request: pointer to the SCSI_IO reqest message frame
2904 *
2905 * Supporting protection 1 and 3.
2906 *
2907 * Returns nothing
2908 */
2909static void
2910_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2911{
2912 u16 eedp_flags;
2913 unsigned char prot_op = scsi_get_prot_op(scmd);
2914 unsigned char prot_type = scsi_get_prot_type(scmd);
2915
2916 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2917 prot_type == SCSI_PROT_DIF_TYPE2 ||
2918 prot_op == SCSI_PROT_NORMAL)
2919 return;
2920
2921 if (prot_op == SCSI_PROT_READ_STRIP)
2922 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2923 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2924 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2925 else
2926 return;
2927
Eric Moore3c621b32009-05-18 12:59:41 -06002928 switch (prot_type) {
2929 case SCSI_PROT_DIF_TYPE1:
2930
2931 /*
2932 * enable ref/guard checking
2933 * auto increment ref tag
2934 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05302935 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06002936 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2937 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2938 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2939 cpu_to_be32(scsi_get_lba(scmd));
2940
2941 break;
2942
2943 case SCSI_PROT_DIF_TYPE3:
2944
2945 /*
2946 * enable guard checking
2947 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05302948 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06002949 break;
2950 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05302951 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
2952 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06002953}
2954
2955/**
2956 * _scsih_eedp_error_handling - return sense code for EEDP errors
2957 * @scmd: pointer to scsi command object
2958 * @ioc_status: ioc status
2959 *
2960 * Returns nothing
2961 */
2962static void
2963_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2964{
2965 u8 ascq;
2966 u8 sk;
2967 u8 host_byte;
2968
2969 switch (ioc_status) {
2970 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2971 ascq = 0x01;
2972 break;
2973 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2974 ascq = 0x02;
2975 break;
2976 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2977 ascq = 0x03;
2978 break;
2979 default:
2980 ascq = 0x00;
2981 break;
2982 }
2983
2984 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2985 sk = ILLEGAL_REQUEST;
2986 host_byte = DID_ABORT;
2987 } else {
2988 sk = ABORTED_COMMAND;
2989 host_byte = DID_OK;
2990 }
2991
2992 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2993 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2994 SAM_STAT_CHECK_CONDITION;
2995}
2996
2997/**
Eric Moored5d135b2009-05-18 13:02:08 -06002998 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06002999 * @scmd: pointer to scsi command object
3000 * @done: function pointer to be invoked on completion
3001 *
3002 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3003 *
3004 * Returns 0 on success. If there's a failure, return either:
3005 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3006 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3007 */
3008static int
Eric Moored5d135b2009-05-18 13:02:08 -06003009_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003010{
3011 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3012 struct MPT2SAS_DEVICE *sas_device_priv_data;
3013 struct MPT2SAS_TARGET *sas_target_priv_data;
3014 Mpi2SCSIIORequest_t *mpi_request;
3015 u32 mpi_control;
3016 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003017
3018 scmd->scsi_done = done;
3019 sas_device_priv_data = scmd->device->hostdata;
3020 if (!sas_device_priv_data) {
3021 scmd->result = DID_NO_CONNECT << 16;
3022 scmd->scsi_done(scmd);
3023 return 0;
3024 }
3025
3026 sas_target_priv_data = sas_device_priv_data->sas_target;
3027 if (!sas_target_priv_data || sas_target_priv_data->handle ==
3028 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
3029 scmd->result = DID_NO_CONNECT << 16;
3030 scmd->scsi_done(scmd);
3031 return 0;
3032 }
3033
3034 /* see if we are busy with task managment stuff */
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303035 if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303036 return SCSI_MLQUEUE_DEVICE_BUSY;
3037 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003038 return SCSI_MLQUEUE_HOST_BUSY;
Eric Moore635374e2009-03-09 01:21:12 -06003039
3040 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3041 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3042 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3043 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3044 else
3045 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3046
3047 /* set tags */
3048 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3049 if (scmd->device->tagged_supported) {
3050 if (scmd->device->ordered_tags)
3051 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3052 else
3053 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3054 } else
3055/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3056/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3057 */
3058 mpi_control |= (0x500);
3059
3060 } else
3061 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303062 /* Make sure Device is not raid volume */
3063 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3064 sas_is_tlr_enabled(scmd->device))
Eric Moore635374e2009-03-09 01:21:12 -06003065 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3066
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303067 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003068 if (!smid) {
3069 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3070 ioc->name, __func__);
3071 goto out;
3072 }
3073 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3074 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003075 _scsih_setup_eedp(scmd, mpi_request);
Eric Moore635374e2009-03-09 01:21:12 -06003076 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3077 if (sas_device_priv_data->sas_target->flags &
3078 MPT_TARGET_FLAGS_RAID_COMPONENT)
3079 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3080 else
3081 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3082 mpi_request->DevHandle =
3083 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3084 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3085 mpi_request->Control = cpu_to_le32(mpi_control);
3086 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3087 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3088 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3089 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303090 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003091 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3092 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3093 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303094 mpi_request->VF_ID = 0; /* TODO */
3095 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003096 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3097 mpi_request->LUN);
3098 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3099
3100 if (!mpi_request->DataLength) {
3101 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3102 } else {
3103 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3104 mpt2sas_base_free_smid(ioc, smid);
3105 goto out;
3106 }
3107 }
3108
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303109 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Eric Moore635374e2009-03-09 01:21:12 -06003110 sas_device_priv_data->sas_target->handle);
3111 return 0;
3112
3113 out:
3114 return SCSI_MLQUEUE_HOST_BUSY;
3115}
3116
3117/**
3118 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3119 * @sense_buffer: sense data returned by target
3120 * @data: normalized skey/asc/ascq
3121 *
3122 * Return nothing.
3123 */
3124static void
3125_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3126{
3127 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3128 /* descriptor format */
3129 data->skey = sense_buffer[1] & 0x0F;
3130 data->asc = sense_buffer[2];
3131 data->ascq = sense_buffer[3];
3132 } else {
3133 /* fixed format */
3134 data->skey = sense_buffer[2] & 0x0F;
3135 data->asc = sense_buffer[12];
3136 data->ascq = sense_buffer[13];
3137 }
3138}
3139
3140#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3141/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003142 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003143 * @ioc: per adapter object
3144 * @scmd: pointer to scsi command object
3145 * @mpi_reply: reply mf payload returned from firmware
3146 *
3147 * scsi_status - SCSI Status code returned from target device
3148 * scsi_state - state info associated with SCSI_IO determined by ioc
3149 * ioc_status - ioc supplied status info
3150 *
3151 * Return nothing.
3152 */
3153static void
3154_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3155 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3156{
3157 u32 response_info;
3158 u8 *response_bytes;
3159 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3160 MPI2_IOCSTATUS_MASK;
3161 u8 scsi_state = mpi_reply->SCSIState;
3162 u8 scsi_status = mpi_reply->SCSIStatus;
3163 char *desc_ioc_state = NULL;
3164 char *desc_scsi_status = NULL;
3165 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303166 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3167
3168 if (log_info == 0x31170000)
3169 return;
Eric Moore635374e2009-03-09 01:21:12 -06003170
3171 switch (ioc_status) {
3172 case MPI2_IOCSTATUS_SUCCESS:
3173 desc_ioc_state = "success";
3174 break;
3175 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3176 desc_ioc_state = "invalid function";
3177 break;
3178 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3179 desc_ioc_state = "scsi recovered error";
3180 break;
3181 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3182 desc_ioc_state = "scsi invalid dev handle";
3183 break;
3184 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3185 desc_ioc_state = "scsi device not there";
3186 break;
3187 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3188 desc_ioc_state = "scsi data overrun";
3189 break;
3190 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3191 desc_ioc_state = "scsi data underrun";
3192 break;
3193 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3194 desc_ioc_state = "scsi io data error";
3195 break;
3196 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3197 desc_ioc_state = "scsi protocol error";
3198 break;
3199 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3200 desc_ioc_state = "scsi task terminated";
3201 break;
3202 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3203 desc_ioc_state = "scsi residual mismatch";
3204 break;
3205 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3206 desc_ioc_state = "scsi task mgmt failed";
3207 break;
3208 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3209 desc_ioc_state = "scsi ioc terminated";
3210 break;
3211 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3212 desc_ioc_state = "scsi ext terminated";
3213 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003214 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3215 desc_ioc_state = "eedp guard error";
3216 break;
3217 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3218 desc_ioc_state = "eedp ref tag error";
3219 break;
3220 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3221 desc_ioc_state = "eedp app tag error";
3222 break;
Eric Moore635374e2009-03-09 01:21:12 -06003223 default:
3224 desc_ioc_state = "unknown";
3225 break;
3226 }
3227
3228 switch (scsi_status) {
3229 case MPI2_SCSI_STATUS_GOOD:
3230 desc_scsi_status = "good";
3231 break;
3232 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3233 desc_scsi_status = "check condition";
3234 break;
3235 case MPI2_SCSI_STATUS_CONDITION_MET:
3236 desc_scsi_status = "condition met";
3237 break;
3238 case MPI2_SCSI_STATUS_BUSY:
3239 desc_scsi_status = "busy";
3240 break;
3241 case MPI2_SCSI_STATUS_INTERMEDIATE:
3242 desc_scsi_status = "intermediate";
3243 break;
3244 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3245 desc_scsi_status = "intermediate condmet";
3246 break;
3247 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3248 desc_scsi_status = "reservation conflict";
3249 break;
3250 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3251 desc_scsi_status = "command terminated";
3252 break;
3253 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3254 desc_scsi_status = "task set full";
3255 break;
3256 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3257 desc_scsi_status = "aca active";
3258 break;
3259 case MPI2_SCSI_STATUS_TASK_ABORTED:
3260 desc_scsi_status = "task aborted";
3261 break;
3262 default:
3263 desc_scsi_status = "unknown";
3264 break;
3265 }
3266
3267 desc_scsi_state[0] = '\0';
3268 if (!scsi_state)
3269 desc_scsi_state = " ";
3270 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3271 strcat(desc_scsi_state, "response info ");
3272 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3273 strcat(desc_scsi_state, "state terminated ");
3274 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3275 strcat(desc_scsi_state, "no status ");
3276 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3277 strcat(desc_scsi_state, "autosense failed ");
3278 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3279 strcat(desc_scsi_state, "autosense valid ");
3280
3281 scsi_print_command(scmd);
3282 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3283 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3284 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3285 ioc_status, smid);
3286 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3287 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3288 scsi_get_resid(scmd));
3289 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3290 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3291 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3292 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3293 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3294 scsi_status, desc_scsi_state, scsi_state);
3295
3296 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3297 struct sense_info data;
3298 _scsih_normalize_sense(scmd->sense_buffer, &data);
3299 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3300 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
3301 data.asc, data.ascq);
3302 }
3303
3304 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3305 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3306 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303307 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003308 }
3309}
3310#endif
3311
3312/**
3313 * _scsih_smart_predicted_fault - illuminate Fault LED
3314 * @ioc: per adapter object
3315 * @handle: device handle
3316 *
3317 * Return nothing.
3318 */
3319static void
3320_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3321{
3322 Mpi2SepReply_t mpi_reply;
3323 Mpi2SepRequest_t mpi_request;
3324 struct scsi_target *starget;
3325 struct MPT2SAS_TARGET *sas_target_priv_data;
3326 Mpi2EventNotificationReply_t *event_reply;
3327 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3328 struct _sas_device *sas_device;
3329 ssize_t sz;
3330 unsigned long flags;
3331
3332 /* only handle non-raid devices */
3333 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3334 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3335 if (!sas_device) {
3336 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3337 return;
3338 }
3339 starget = sas_device->starget;
3340 sas_target_priv_data = starget->hostdata;
3341
3342 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3343 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3344 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3345 return;
3346 }
3347 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3348 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3349
3350 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3351 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3352 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3353 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3354 mpi_request.SlotStatus =
3355 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
3356 mpi_request.DevHandle = cpu_to_le16(handle);
3357 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3358 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3359 &mpi_request)) != 0) {
3360 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3361 ioc->name, __FILE__, __LINE__, __func__);
3362 return;
3363 }
3364
3365 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3366 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3367 "enclosure_processor: ioc_status (0x%04x), "
3368 "loginfo(0x%08x)\n", ioc->name,
3369 le16_to_cpu(mpi_reply.IOCStatus),
3370 le32_to_cpu(mpi_reply.IOCLogInfo)));
3371 return;
3372 }
3373 }
3374
3375 /* insert into event log */
3376 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3377 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3378 event_reply = kzalloc(sz, GFP_KERNEL);
3379 if (!event_reply) {
3380 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3381 ioc->name, __FILE__, __LINE__, __func__);
3382 return;
3383 }
3384
3385 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3386 event_reply->Event =
3387 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3388 event_reply->MsgLength = sz/4;
3389 event_reply->EventDataLength =
3390 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3391 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3392 event_reply->EventData;
3393 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3394 event_data->ASC = 0x5D;
3395 event_data->DevHandle = cpu_to_le16(handle);
3396 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3397 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3398 kfree(event_reply);
3399}
3400
3401/**
Eric Moored5d135b2009-05-18 13:02:08 -06003402 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003403 * @ioc: per adapter object
3404 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303405 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003406 * @reply: reply message frame(lower 32bit addr)
3407 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303408 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003409 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303410 * Return 1 meaning mf should be freed from _base_interrupt
3411 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003412 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303413static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303414_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003415{
3416 Mpi2SCSIIORequest_t *mpi_request;
3417 Mpi2SCSIIOReply_t *mpi_reply;
3418 struct scsi_cmnd *scmd;
3419 u16 ioc_status;
3420 u32 xfer_cnt;
3421 u8 scsi_state;
3422 u8 scsi_status;
3423 u32 log_info;
3424 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303425 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003426
3427 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303428 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003429 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303430 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003431
3432 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3433
3434 if (mpi_reply == NULL) {
3435 scmd->result = DID_OK << 16;
3436 goto out;
3437 }
3438
3439 sas_device_priv_data = scmd->device->hostdata;
3440 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3441 sas_device_priv_data->sas_target->deleted) {
3442 scmd->result = DID_NO_CONNECT << 16;
3443 goto out;
3444 }
3445
3446 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303447 scsi_state = mpi_reply->SCSIState;
3448 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3449 response_code =
3450 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003451 if (!sas_device_priv_data->tlr_snoop_check) {
3452 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303453 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3454 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05303455 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3456 sas_disable_tlr(scmd->device);
3457 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3458 }
Eric Moore635374e2009-03-09 01:21:12 -06003459 }
3460
3461 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3462 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3463 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3464 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3465 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3466 else
3467 log_info = 0;
3468 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003469 scsi_status = mpi_reply->SCSIStatus;
3470
3471 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3472 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3473 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3474 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3475 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3476 }
3477
3478 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3479 struct sense_info data;
3480 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3481 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003482 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003483 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003484 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003485 _scsih_normalize_sense(scmd->sense_buffer, &data);
3486 /* failure prediction threshold exceeded */
3487 if (data.asc == 0x5D)
3488 _scsih_smart_predicted_fault(ioc,
3489 le16_to_cpu(mpi_reply->DevHandle));
3490 }
3491
3492 switch (ioc_status) {
3493 case MPI2_IOCSTATUS_BUSY:
3494 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3495 scmd->result = SAM_STAT_BUSY;
3496 break;
3497
3498 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3499 scmd->result = DID_NO_CONNECT << 16;
3500 break;
3501
3502 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3503 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303504 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3505 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003506 }
Eric Moore635374e2009-03-09 01:21:12 -06003507 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3508 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3509 scmd->result = DID_RESET << 16;
3510 break;
3511
3512 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3513 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3514 scmd->result = DID_SOFT_ERROR << 16;
3515 else
3516 scmd->result = (DID_OK << 16) | scsi_status;
3517 break;
3518
3519 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3520 scmd->result = (DID_OK << 16) | scsi_status;
3521
3522 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3523 break;
3524
3525 if (xfer_cnt < scmd->underflow) {
3526 if (scsi_status == SAM_STAT_BUSY)
3527 scmd->result = SAM_STAT_BUSY;
3528 else
3529 scmd->result = DID_SOFT_ERROR << 16;
3530 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3531 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3532 scmd->result = DID_SOFT_ERROR << 16;
3533 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3534 scmd->result = DID_RESET << 16;
3535 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3536 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3537 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3538 scmd->result = (DRIVER_SENSE << 24) |
3539 SAM_STAT_CHECK_CONDITION;
3540 scmd->sense_buffer[0] = 0x70;
3541 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3542 scmd->sense_buffer[12] = 0x20;
3543 scmd->sense_buffer[13] = 0;
3544 }
3545 break;
3546
3547 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3548 scsi_set_resid(scmd, 0);
3549 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3550 case MPI2_IOCSTATUS_SUCCESS:
3551 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303552 if (response_code ==
3553 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3554 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3555 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003556 scmd->result = DID_SOFT_ERROR << 16;
3557 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3558 scmd->result = DID_RESET << 16;
3559 break;
3560
Eric Moore3c621b32009-05-18 12:59:41 -06003561 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3562 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3563 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3564 _scsih_eedp_error_handling(scmd, ioc_status);
3565 break;
Eric Moore635374e2009-03-09 01:21:12 -06003566 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3567 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3568 case MPI2_IOCSTATUS_INVALID_SGL:
3569 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3570 case MPI2_IOCSTATUS_INVALID_FIELD:
3571 case MPI2_IOCSTATUS_INVALID_STATE:
3572 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3573 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3574 default:
3575 scmd->result = DID_SOFT_ERROR << 16;
3576 break;
3577
3578 }
3579
3580#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3581 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3582 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3583#endif
3584
3585 out:
3586 scsi_dma_unmap(scmd);
3587 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303588 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003589}
3590
3591/**
Eric Moore635374e2009-03-09 01:21:12 -06003592 * _scsih_sas_host_refresh - refreshing sas host object contents
3593 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003594 * Context: user
3595 *
3596 * During port enable, fw will send topology events for every device. Its
3597 * possible that the handles may change from the previous setting, so this
3598 * code keeping handles updating if changed.
3599 *
3600 * Return nothing.
3601 */
3602static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303603_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06003604{
3605 u16 sz;
3606 u16 ioc_status;
3607 int i;
3608 Mpi2ConfigReply_t mpi_reply;
3609 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303610 u16 attached_handle;
Eric Moore635374e2009-03-09 01:21:12 -06003611
3612 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3613 "updating handles for sas_host(0x%016llx)\n",
3614 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3615
3616 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3617 * sizeof(Mpi2SasIOUnit0PhyData_t));
3618 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3619 if (!sas_iounit_pg0) {
3620 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3621 ioc->name, __FILE__, __LINE__, __func__);
3622 return;
3623 }
Eric Moore635374e2009-03-09 01:21:12 -06003624
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303625 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3626 sas_iounit_pg0, sz)) != 0)
3627 goto out;
3628 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3629 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3630 goto out;
3631 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3632 if (i == 0)
3633 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3634 PhyData[0].ControllerDevHandle);
3635 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3636 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3637 AttachedDevHandle);
3638 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3639 attached_handle, i, sas_iounit_pg0->PhyData[i].
3640 NegotiatedLinkRate >> 4);
3641 }
Eric Moore635374e2009-03-09 01:21:12 -06003642 out:
3643 kfree(sas_iounit_pg0);
3644}
3645
3646/**
3647 * _scsih_sas_host_add - create sas host object
3648 * @ioc: per adapter object
3649 *
3650 * Creating host side data object, stored in ioc->sas_hba
3651 *
3652 * Return nothing.
3653 */
3654static void
3655_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3656{
3657 int i;
3658 Mpi2ConfigReply_t mpi_reply;
3659 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3660 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3661 Mpi2SasPhyPage0_t phy_pg0;
3662 Mpi2SasDevicePage0_t sas_device_pg0;
3663 Mpi2SasEnclosurePage0_t enclosure_pg0;
3664 u16 ioc_status;
3665 u16 sz;
3666 u16 device_missing_delay;
3667
3668 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3669 if (!ioc->sas_hba.num_phys) {
3670 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3671 ioc->name, __FILE__, __LINE__, __func__);
3672 return;
3673 }
3674
3675 /* sas_iounit page 0 */
3676 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3677 sizeof(Mpi2SasIOUnit0PhyData_t));
3678 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3679 if (!sas_iounit_pg0) {
3680 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3681 ioc->name, __FILE__, __LINE__, __func__);
3682 return;
3683 }
3684 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3685 sas_iounit_pg0, sz))) {
3686 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3687 ioc->name, __FILE__, __LINE__, __func__);
3688 goto out;
3689 }
3690 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3691 MPI2_IOCSTATUS_MASK;
3692 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3693 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3694 ioc->name, __FILE__, __LINE__, __func__);
3695 goto out;
3696 }
3697
3698 /* sas_iounit page 1 */
3699 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3700 sizeof(Mpi2SasIOUnit1PhyData_t));
3701 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3702 if (!sas_iounit_pg1) {
3703 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3704 ioc->name, __FILE__, __LINE__, __func__);
3705 goto out;
3706 }
3707 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3708 sas_iounit_pg1, sz))) {
3709 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3710 ioc->name, __FILE__, __LINE__, __func__);
3711 goto out;
3712 }
3713 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3714 MPI2_IOCSTATUS_MASK;
3715 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3716 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3717 ioc->name, __FILE__, __LINE__, __func__);
3718 goto out;
3719 }
3720
3721 ioc->io_missing_delay =
3722 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3723 device_missing_delay =
3724 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3725 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3726 ioc->device_missing_delay = (device_missing_delay &
3727 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3728 else
3729 ioc->device_missing_delay = device_missing_delay &
3730 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3731
3732 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3733 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3734 sizeof(struct _sas_phy), GFP_KERNEL);
3735 if (!ioc->sas_hba.phy) {
3736 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3737 ioc->name, __FILE__, __LINE__, __func__);
3738 goto out;
3739 }
3740 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3741 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3742 i))) {
3743 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3744 ioc->name, __FILE__, __LINE__, __func__);
3745 goto out;
3746 }
3747 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3748 MPI2_IOCSTATUS_MASK;
3749 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3750 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3751 ioc->name, __FILE__, __LINE__, __func__);
3752 goto out;
3753 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303754
3755 if (i == 0)
3756 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3757 PhyData[0].ControllerDevHandle);
3758 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06003759 ioc->sas_hba.phy[i].phy_id = i;
3760 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3761 phy_pg0, ioc->sas_hba.parent_dev);
3762 }
3763 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303764 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06003765 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3766 ioc->name, __FILE__, __LINE__, __func__);
3767 goto out;
3768 }
Eric Moore635374e2009-03-09 01:21:12 -06003769 ioc->sas_hba.enclosure_handle =
3770 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3771 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3772 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3773 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3774 (unsigned long long) ioc->sas_hba.sas_address,
3775 ioc->sas_hba.num_phys) ;
3776
3777 if (ioc->sas_hba.enclosure_handle) {
3778 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3779 &enclosure_pg0,
3780 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3781 ioc->sas_hba.enclosure_handle))) {
3782 ioc->sas_hba.enclosure_logical_id =
3783 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3784 }
3785 }
3786
3787 out:
3788 kfree(sas_iounit_pg1);
3789 kfree(sas_iounit_pg0);
3790}
3791
3792/**
3793 * _scsih_expander_add - creating expander object
3794 * @ioc: per adapter object
3795 * @handle: expander handle
3796 *
3797 * Creating expander object, stored in ioc->sas_expander_list.
3798 *
3799 * Return 0 for success, else error.
3800 */
3801static int
3802_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3803{
3804 struct _sas_node *sas_expander;
3805 Mpi2ConfigReply_t mpi_reply;
3806 Mpi2ExpanderPage0_t expander_pg0;
3807 Mpi2ExpanderPage1_t expander_pg1;
3808 Mpi2SasEnclosurePage0_t enclosure_pg0;
3809 u32 ioc_status;
3810 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303811 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003812 int i;
3813 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303814 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003815 int rc = 0;
3816
3817 if (!handle)
3818 return -1;
3819
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303820 if (ioc->shost_recovery)
3821 return -1;
3822
Eric Moore635374e2009-03-09 01:21:12 -06003823 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3824 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3825 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3826 ioc->name, __FILE__, __LINE__, __func__);
3827 return -1;
3828 }
3829
3830 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3831 MPI2_IOCSTATUS_MASK;
3832 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3833 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3834 ioc->name, __FILE__, __LINE__, __func__);
3835 return -1;
3836 }
3837
3838 /* handle out of order topology events */
3839 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303840 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
3841 != 0) {
3842 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3843 ioc->name, __FILE__, __LINE__, __func__);
3844 return -1;
3845 }
3846 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06003847 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303848 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3849 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003850 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3851 if (!sas_expander) {
3852 rc = _scsih_expander_add(ioc, parent_handle);
3853 if (rc != 0)
3854 return rc;
3855 }
3856 }
3857
Eric Moore635374e2009-03-09 01:21:12 -06003858 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303859 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06003860 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3861 sas_address);
3862 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3863
3864 if (sas_expander)
3865 return 0;
3866
3867 sas_expander = kzalloc(sizeof(struct _sas_node),
3868 GFP_KERNEL);
3869 if (!sas_expander) {
3870 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3871 ioc->name, __FILE__, __LINE__, __func__);
3872 return -1;
3873 }
3874
3875 sas_expander->handle = handle;
3876 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303877 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06003878 sas_expander->sas_address = sas_address;
3879
3880 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3881 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303882 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06003883 sas_expander->sas_address, sas_expander->num_phys);
3884
3885 if (!sas_expander->num_phys)
3886 goto out_fail;
3887 sas_expander->phy = kcalloc(sas_expander->num_phys,
3888 sizeof(struct _sas_phy), GFP_KERNEL);
3889 if (!sas_expander->phy) {
3890 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3891 ioc->name, __FILE__, __LINE__, __func__);
3892 rc = -1;
3893 goto out_fail;
3894 }
3895
3896 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3897 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303898 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003899 if (!mpt2sas_port) {
3900 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3901 ioc->name, __FILE__, __LINE__, __func__);
3902 rc = -1;
3903 goto out_fail;
3904 }
3905 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3906
3907 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3908 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3909 &expander_pg1, i, handle))) {
3910 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3911 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05303912 rc = -1;
3913 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06003914 }
3915 sas_expander->phy[i].handle = handle;
3916 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303917
3918 if ((mpt2sas_transport_add_expander_phy(ioc,
3919 &sas_expander->phy[i], expander_pg1,
3920 sas_expander->parent_dev))) {
3921 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3922 ioc->name, __FILE__, __LINE__, __func__);
3923 rc = -1;
3924 goto out_fail;
3925 }
Eric Moore635374e2009-03-09 01:21:12 -06003926 }
3927
3928 if (sas_expander->enclosure_handle) {
3929 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3930 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3931 sas_expander->enclosure_handle))) {
3932 sas_expander->enclosure_logical_id =
3933 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3934 }
3935 }
3936
3937 _scsih_expander_node_add(ioc, sas_expander);
3938 return 0;
3939
3940 out_fail:
3941
Kashyap, Desai20f58952009-08-07 19:34:26 +05303942 if (mpt2sas_port)
3943 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303944 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003945 kfree(sas_expander);
3946 return rc;
3947}
3948
3949/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05303950 * _scsih_done - scsih callback handler.
3951 * @ioc: per adapter object
3952 * @smid: system request message index
3953 * @msix_index: MSIX table index supplied by the OS
3954 * @reply: reply message frame(lower 32bit addr)
3955 *
3956 * Callback handler when sending internal generated message frames.
3957 * The callback index passed is `ioc->scsih_cb_idx`
3958 *
3959 * Return 1 meaning mf should be freed from _base_interrupt
3960 * 0 means the mf is freed from this function.
3961 */
3962static u8
3963_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
3964{
3965 MPI2DefaultReply_t *mpi_reply;
3966
3967 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3968 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
3969 return 1;
3970 if (ioc->scsih_cmds.smid != smid)
3971 return 1;
3972 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
3973 if (mpi_reply) {
3974 memcpy(ioc->scsih_cmds.reply, mpi_reply,
3975 mpi_reply->MsgLength*4);
3976 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
3977 }
3978 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
3979 complete(&ioc->scsih_cmds.done);
3980 return 1;
3981}
3982
3983/**
Eric Moore635374e2009-03-09 01:21:12 -06003984 * _scsih_expander_remove - removing expander object
3985 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303986 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06003987 *
3988 * Return nothing.
3989 */
3990static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303991_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06003992{
3993 struct _sas_node *sas_expander;
3994 unsigned long flags;
3995
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303996 if (ioc->shost_recovery)
3997 return;
3998
Eric Moore635374e2009-03-09 01:21:12 -06003999 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304000 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4001 sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004002 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4003 _scsih_expander_node_remove(ioc, sas_expander);
4004}
4005
4006/**
4007 * _scsih_add_device - creating sas device object
4008 * @ioc: per adapter object
4009 * @handle: sas device handle
4010 * @phy_num: phy number end device attached to
4011 * @is_pd: is this hidden raid component
4012 *
4013 * Creating end device object, stored in ioc->sas_device_list.
4014 *
4015 * Returns 0 for success, non-zero for failure.
4016 */
4017static int
4018_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4019{
4020 Mpi2ConfigReply_t mpi_reply;
4021 Mpi2SasDevicePage0_t sas_device_pg0;
4022 Mpi2SasEnclosurePage0_t enclosure_pg0;
4023 struct _sas_device *sas_device;
4024 u32 ioc_status;
4025 __le64 sas_address;
4026 u32 device_info;
4027 unsigned long flags;
4028
4029 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4030 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4031 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4032 ioc->name, __FILE__, __LINE__, __func__);
4033 return -1;
4034 }
4035
4036 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4037 MPI2_IOCSTATUS_MASK;
4038 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4039 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4040 ioc->name, __FILE__, __LINE__, __func__);
4041 return -1;
4042 }
4043
4044 /* check if device is present */
4045 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4046 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4047 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4048 ioc->name, __FILE__, __LINE__, __func__);
4049 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4050 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4051 return -1;
4052 }
4053
4054 /* check if there were any issus with discovery */
4055 if (sas_device_pg0.AccessStatus ==
4056 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
4057 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4058 ioc->name, __FILE__, __LINE__, __func__);
4059 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
4060 ioc->name, sas_device_pg0.AccessStatus);
4061 return -1;
4062 }
4063
4064 /* check if this is end device */
4065 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4066 if (!(_scsih_is_end_device(device_info))) {
4067 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4068 ioc->name, __FILE__, __LINE__, __func__);
4069 return -1;
4070 }
4071
4072 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4073
4074 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4075 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4076 sas_address);
4077 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4078
4079 if (sas_device) {
4080 _scsih_ublock_io_device(ioc, handle);
4081 return 0;
4082 }
4083
4084 sas_device = kzalloc(sizeof(struct _sas_device),
4085 GFP_KERNEL);
4086 if (!sas_device) {
4087 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4088 ioc->name, __FILE__, __LINE__, __func__);
4089 return -1;
4090 }
4091
4092 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304093 if (_scsih_get_sas_address(ioc, le16_to_cpu
4094 (sas_device_pg0.ParentDevHandle),
4095 &sas_device->sas_address_parent) != 0)
4096 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4097 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004098 sas_device->enclosure_handle =
4099 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4100 sas_device->slot =
4101 le16_to_cpu(sas_device_pg0.Slot);
4102 sas_device->device_info = device_info;
4103 sas_device->sas_address = sas_address;
4104 sas_device->hidden_raid_component = is_pd;
4105
4106 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05304107 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4108 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4109 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06004110 sas_device->enclosure_logical_id =
4111 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06004112
4113 /* get device name */
4114 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4115
4116 if (ioc->wait_for_port_enable_to_complete)
4117 _scsih_sas_device_init_add(ioc, sas_device);
4118 else
4119 _scsih_sas_device_add(ioc, sas_device);
4120
4121 return 0;
4122}
4123
4124/**
4125 * _scsih_remove_device - removing sas device object
4126 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304127 * @sas_device: the sas_device object
Eric Moore635374e2009-03-09 01:21:12 -06004128 *
4129 * Return nothing.
4130 */
4131static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304132_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
4133 *sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004134{
4135 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06004136 Mpi2SasIoUnitControlReply_t mpi_reply;
4137 Mpi2SasIoUnitControlRequest_t mpi_request;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304138 u16 device_handle, handle;
Eric Moore635374e2009-03-09 01:21:12 -06004139
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304140 if (!sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004141 return;
Eric Moore635374e2009-03-09 01:21:12 -06004142
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304143 handle = sas_device->handle;
4144 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
4145 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
4146 (unsigned long long) sas_device->sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004147
4148 if (sas_device->starget && sas_device->starget->hostdata) {
4149 sas_target_priv_data = sas_device->starget->hostdata;
4150 sas_target_priv_data->deleted = 1;
4151 }
Eric Moore635374e2009-03-09 01:21:12 -06004152
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304153 if (ioc->remove_host || ioc->shost_recovery || !handle)
Eric Moore635374e2009-03-09 01:21:12 -06004154 goto out;
4155
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304156 if ((sas_device->state & MPTSAS_STATE_TR_COMPLETE)) {
4157 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304158 "target_reset handle(0x%04x)\n", ioc->name,
4159 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304160 goto skip_tr;
4161 }
4162
Eric Moore635374e2009-03-09 01:21:12 -06004163 /* Target Reset to flush out all the outstanding IO */
4164 device_handle = (sas_device->hidden_raid_component) ?
4165 sas_device->volume_handle : handle;
4166 if (device_handle) {
4167 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
4168 "handle(0x%04x)\n", ioc->name, device_handle));
4169 mutex_lock(&ioc->tm_cmds.mutex);
4170 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
4171 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
4172 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4173 mutex_unlock(&ioc->tm_cmds.mutex);
4174 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
4175 "done: handle(0x%04x)\n", ioc->name, device_handle));
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304176 if (ioc->shost_recovery)
4177 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004178 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304179 skip_tr:
4180
4181 if ((sas_device->state & MPTSAS_STATE_CNTRL_COMPLETE)) {
4182 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
4183 "sas_cntrl handle(0x%04x)\n", ioc->name, handle));
4184 goto out;
4185 }
Eric Moore635374e2009-03-09 01:21:12 -06004186
4187 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
4188 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
4189 "(0x%04x)\n", ioc->name, handle));
4190 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
4191 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
4192 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
4193 mpi_request.DevHandle = handle;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304194 mpi_request.VF_ID = 0; /* TODO */
4195 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004196 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
4197 &mpi_request)) != 0) {
4198 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4199 ioc->name, __FILE__, __LINE__, __func__);
4200 }
4201
4202 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
4203 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
4204 le16_to_cpu(mpi_reply.IOCStatus),
4205 le32_to_cpu(mpi_reply.IOCLogInfo)));
4206
4207 out:
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304208
4209 _scsih_ublock_io_device(ioc, handle);
4210
Eric Moore635374e2009-03-09 01:21:12 -06004211 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304212 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004213
4214 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304215 "(0x%016llx)\n", ioc->name, handle,
Eric Moore635374e2009-03-09 01:21:12 -06004216 (unsigned long long) sas_device->sas_address);
4217 _scsih_sas_device_remove(ioc, sas_device);
4218
4219 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
4220 "(0x%04x)\n", ioc->name, __func__, handle));
4221}
4222
4223#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4224/**
4225 * _scsih_sas_topology_change_event_debug - debug for topology event
4226 * @ioc: per adapter object
4227 * @event_data: event data payload
4228 * Context: user.
4229 */
4230static void
4231_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4232 Mpi2EventDataSasTopologyChangeList_t *event_data)
4233{
4234 int i;
4235 u16 handle;
4236 u16 reason_code;
4237 u8 phy_number;
4238 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304239 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004240
4241 switch (event_data->ExpStatus) {
4242 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4243 status_str = "add";
4244 break;
4245 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4246 status_str = "remove";
4247 break;
4248 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304249 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004250 status_str = "responding";
4251 break;
4252 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4253 status_str = "remove delay";
4254 break;
4255 default:
4256 status_str = "unknown status";
4257 break;
4258 }
4259 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
4260 ioc->name, status_str);
4261 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
4262 "start_phy(%02d), count(%d)\n",
4263 le16_to_cpu(event_data->ExpanderDevHandle),
4264 le16_to_cpu(event_data->EnclosureHandle),
4265 event_data->StartPhyNum, event_data->NumEntries);
4266 for (i = 0; i < event_data->NumEntries; i++) {
4267 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4268 if (!handle)
4269 continue;
4270 phy_number = event_data->StartPhyNum + i;
4271 reason_code = event_data->PHY[i].PhyStatus &
4272 MPI2_EVENT_SAS_TOPO_RC_MASK;
4273 switch (reason_code) {
4274 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304275 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004276 break;
4277 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304278 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004279 break;
4280 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304281 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004282 break;
4283 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304284 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004285 break;
4286 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304287 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004288 break;
4289 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304290 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004291 break;
4292 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304293 link_rate = event_data->PHY[i].LinkRate >> 4;
4294 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4295 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x): %s:"
4296 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4297 handle, status_str, link_rate, prev_link_rate);
4298
Eric Moore635374e2009-03-09 01:21:12 -06004299 }
4300}
4301#endif
4302
4303/**
4304 * _scsih_sas_topology_change_event - handle topology changes
4305 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304306 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004307 * Context: user.
4308 *
4309 */
4310static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304311_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004312 struct fw_event_work *fw_event)
4313{
4314 int i;
4315 u16 parent_handle, handle;
4316 u16 reason_code;
4317 u8 phy_number;
4318 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304319 struct _sas_device *sas_device;
4320 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004321 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304322 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304323 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004324
4325#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4326 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4327 _scsih_sas_topology_change_event_debug(ioc, event_data);
4328#endif
4329
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304330 if (ioc->shost_recovery)
4331 return;
4332
Eric Moore635374e2009-03-09 01:21:12 -06004333 if (!ioc->sas_hba.num_phys)
4334 _scsih_sas_host_add(ioc);
4335 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304336 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004337
4338 if (fw_event->ignore) {
4339 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
4340 "event\n", ioc->name));
4341 return;
4342 }
4343
4344 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4345
4346 /* handle expander add */
4347 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4348 if (_scsih_expander_add(ioc, parent_handle) != 0)
4349 return;
4350
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304351 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4352 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4353 parent_handle);
4354 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4355 if (sas_expander)
4356 sas_address = sas_expander->sas_address;
4357 else if (parent_handle < ioc->sas_hba.num_phys)
4358 sas_address = ioc->sas_hba.sas_address;
4359 else
4360 return;
4361
Eric Moore635374e2009-03-09 01:21:12 -06004362 /* handle siblings events */
4363 for (i = 0; i < event_data->NumEntries; i++) {
4364 if (fw_event->ignore) {
4365 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
4366 "expander event\n", ioc->name));
4367 return;
4368 }
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304369 if (ioc->shost_recovery)
4370 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304371 phy_number = event_data->StartPhyNum + i;
4372 reason_code = event_data->PHY[i].PhyStatus &
4373 MPI2_EVENT_SAS_TOPO_RC_MASK;
4374 if ((event_data->PHY[i].PhyStatus &
4375 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4376 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004377 continue;
4378 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4379 if (!handle)
4380 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304381 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304382 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004383 switch (reason_code) {
4384 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304385
4386 if (link_rate == prev_link_rate)
4387 break;
4388
4389 mpt2sas_transport_update_links(ioc, sas_address,
4390 handle, phy_number, link_rate);
4391
4392 if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
4393 _scsih_ublock_io_device(ioc, handle);
4394 break;
Eric Moore635374e2009-03-09 01:21:12 -06004395 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304396
4397 mpt2sas_transport_update_links(ioc, sas_address,
4398 handle, phy_number, link_rate);
4399
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304400 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004401 break;
4402 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304403
4404 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4405 sas_device = _scsih_sas_device_find_by_handle(ioc,
4406 handle);
4407 if (!sas_device) {
4408 spin_unlock_irqrestore(&ioc->sas_device_lock,
4409 flags);
4410 break;
4411 }
4412 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4413 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004414 break;
4415 }
4416 }
4417
4418 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304419 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4420 sas_expander)
4421 _scsih_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004422
4423}
4424
4425#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4426/**
4427 * _scsih_sas_device_status_change_event_debug - debug for device event
4428 * @event_data: event data payload
4429 * Context: user.
4430 *
4431 * Return nothing.
4432 */
4433static void
4434_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4435 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4436{
4437 char *reason_str = NULL;
4438
4439 switch (event_data->ReasonCode) {
4440 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4441 reason_str = "smart data";
4442 break;
4443 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4444 reason_str = "unsupported device discovered";
4445 break;
4446 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4447 reason_str = "internal device reset";
4448 break;
4449 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4450 reason_str = "internal task abort";
4451 break;
4452 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4453 reason_str = "internal task abort set";
4454 break;
4455 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4456 reason_str = "internal clear task set";
4457 break;
4458 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4459 reason_str = "internal query task";
4460 break;
4461 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4462 reason_str = "sata init failure";
4463 break;
4464 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4465 reason_str = "internal device reset complete";
4466 break;
4467 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4468 reason_str = "internal task abort complete";
4469 break;
4470 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4471 reason_str = "internal async notification";
4472 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304473 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4474 reason_str = "expander reduced functionality";
4475 break;
4476 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4477 reason_str = "expander reduced functionality complete";
4478 break;
Eric Moore635374e2009-03-09 01:21:12 -06004479 default:
4480 reason_str = "unknown reason";
4481 break;
4482 }
4483 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
4484 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4485 reason_str, le16_to_cpu(event_data->DevHandle),
4486 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4487 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4488 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4489 event_data->ASC, event_data->ASCQ);
4490 printk(KERN_INFO "\n");
4491}
4492#endif
4493
4494/**
4495 * _scsih_sas_device_status_change_event - handle device status change
4496 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304497 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004498 * Context: user.
4499 *
4500 * Return nothing.
4501 */
4502static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304503_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4504 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004505{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304506 struct MPT2SAS_TARGET *target_priv_data;
4507 struct _sas_device *sas_device;
4508 __le64 sas_address;
4509 unsigned long flags;
4510 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4511 fw_event->event_data;
4512
Eric Moore635374e2009-03-09 01:21:12 -06004513#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4514 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304515 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304516 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004517#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304518
4519 if (!(event_data->ReasonCode ==
4520 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
4521 event_data->ReasonCode ==
4522 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET))
4523 return;
4524
4525 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4526 sas_address = le64_to_cpu(event_data->SASAddress);
4527 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4528 sas_address);
4529 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4530
4531 if (!sas_device || !sas_device->starget)
4532 return;
4533
4534 target_priv_data = sas_device->starget->hostdata;
4535 if (!target_priv_data)
4536 return;
4537
4538 if (event_data->ReasonCode ==
4539 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
4540 target_priv_data->tm_busy = 1;
4541 else
4542 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004543}
4544
4545#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4546/**
4547 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4548 * @ioc: per adapter object
4549 * @event_data: event data payload
4550 * Context: user.
4551 *
4552 * Return nothing.
4553 */
4554static void
4555_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4556 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4557{
4558 char *reason_str = NULL;
4559
4560 switch (event_data->ReasonCode) {
4561 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4562 reason_str = "enclosure add";
4563 break;
4564 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4565 reason_str = "enclosure remove";
4566 break;
4567 default:
4568 reason_str = "unknown reason";
4569 break;
4570 }
4571
4572 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4573 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4574 " number slots(%d)\n", ioc->name, reason_str,
4575 le16_to_cpu(event_data->EnclosureHandle),
4576 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4577 le16_to_cpu(event_data->StartSlot));
4578}
4579#endif
4580
4581/**
4582 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4583 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304584 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004585 * Context: user.
4586 *
4587 * Return nothing.
4588 */
4589static void
4590_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304591 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004592{
4593#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4594 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4595 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304596 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004597#endif
4598}
4599
4600/**
4601 * _scsih_sas_broadcast_primative_event - handle broadcast events
4602 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304603 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004604 * Context: user.
4605 *
4606 * Return nothing.
4607 */
4608static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304609_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4610 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004611{
4612 struct scsi_cmnd *scmd;
4613 u16 smid, handle;
4614 u32 lun;
4615 struct MPT2SAS_DEVICE *sas_device_priv_data;
4616 u32 termination_count;
4617 u32 query_count;
4618 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304619#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4620 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4621#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05304622 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06004623 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4624 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4625 event_data->PortWidth));
Eric Moore635374e2009-03-09 01:21:12 -06004626 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4627 __func__));
4628
4629 mutex_lock(&ioc->tm_cmds.mutex);
4630 termination_count = 0;
4631 query_count = 0;
4632 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304633 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06004634 scmd = _scsih_scsi_lookup_get(ioc, smid);
4635 if (!scmd)
4636 continue;
4637 sas_device_priv_data = scmd->device->hostdata;
4638 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4639 continue;
4640 /* skip hidden raid components */
4641 if (sas_device_priv_data->sas_target->flags &
4642 MPT_TARGET_FLAGS_RAID_COMPONENT)
4643 continue;
4644 /* skip volumes */
4645 if (sas_device_priv_data->sas_target->flags &
4646 MPT_TARGET_FLAGS_VOLUME)
4647 continue;
4648
4649 handle = sas_device_priv_data->sas_target->handle;
4650 lun = sas_device_priv_data->lun;
4651 query_count++;
4652
4653 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4654 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
Eric Moore8901cbb2009-04-21 15:41:32 -06004655 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05304656 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
4657 & MPI2_IOCSTATUS_MASK;
4658 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06004659 (mpi_reply->ResponseCode ==
4660 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4661 mpi_reply->ResponseCode ==
4662 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4663 continue;
4664
4665 mpt2sas_scsih_issue_tm(ioc, handle, lun,
Eric Moore8901cbb2009-04-21 15:41:32 -06004666 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4667 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004668 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4669 }
Eric Moore635374e2009-03-09 01:21:12 -06004670 ioc->broadcast_aen_busy = 0;
4671 mutex_unlock(&ioc->tm_cmds.mutex);
4672
4673 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4674 "%s - exit, query_count = %d termination_count = %d\n",
4675 ioc->name, __func__, query_count, termination_count));
4676}
4677
4678/**
4679 * _scsih_sas_discovery_event - handle discovery events
4680 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304681 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004682 * Context: user.
4683 *
4684 * Return nothing.
4685 */
4686static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304687_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
4688 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004689{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304690 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
4691
Eric Moore635374e2009-03-09 01:21:12 -06004692#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4693 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4694 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4695 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4696 "start" : "stop");
4697 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304698 printk("discovery_status(0x%08x)",
4699 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06004700 printk("\n");
4701 }
4702#endif
4703
4704 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4705 !ioc->sas_hba.num_phys)
4706 _scsih_sas_host_add(ioc);
4707}
4708
4709/**
4710 * _scsih_reprobe_lun - reprobing lun
4711 * @sdev: scsi device struct
4712 * @no_uld_attach: sdev->no_uld_attach flag setting
4713 *
4714 **/
4715static void
4716_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4717{
4718 int rc;
4719
4720 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4721 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4722 sdev->no_uld_attach ? "hidding" : "exposing");
4723 rc = scsi_device_reprobe(sdev);
4724}
4725
4726/**
4727 * _scsih_reprobe_target - reprobing target
4728 * @starget: scsi target struct
4729 * @no_uld_attach: sdev->no_uld_attach flag setting
4730 *
4731 * Note: no_uld_attach flag determines whether the disk device is attached
4732 * to block layer. A value of `1` means to not attach.
4733 **/
4734static void
4735_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4736{
4737 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4738
4739 if (no_uld_attach)
4740 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4741 else
4742 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4743
4744 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4745 _scsih_reprobe_lun);
4746}
4747/**
4748 * _scsih_sas_volume_add - add new volume
4749 * @ioc: per adapter object
4750 * @element: IR config element data
4751 * Context: user.
4752 *
4753 * Return nothing.
4754 */
4755static void
4756_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4757 Mpi2EventIrConfigElement_t *element)
4758{
4759 struct _raid_device *raid_device;
4760 unsigned long flags;
4761 u64 wwid;
4762 u16 handle = le16_to_cpu(element->VolDevHandle);
4763 int rc;
4764
Eric Moore635374e2009-03-09 01:21:12 -06004765 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4766 if (!wwid) {
4767 printk(MPT2SAS_ERR_FMT
4768 "failure at %s:%d/%s()!\n", ioc->name,
4769 __FILE__, __LINE__, __func__);
4770 return;
4771 }
4772
4773 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4774 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4775 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4776
4777 if (raid_device)
4778 return;
4779
4780 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4781 if (!raid_device) {
4782 printk(MPT2SAS_ERR_FMT
4783 "failure at %s:%d/%s()!\n", ioc->name,
4784 __FILE__, __LINE__, __func__);
4785 return;
4786 }
4787
4788 raid_device->id = ioc->sas_id++;
4789 raid_device->channel = RAID_CHANNEL;
4790 raid_device->handle = handle;
4791 raid_device->wwid = wwid;
4792 _scsih_raid_device_add(ioc, raid_device);
4793 if (!ioc->wait_for_port_enable_to_complete) {
4794 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4795 raid_device->id, 0);
4796 if (rc)
4797 _scsih_raid_device_remove(ioc, raid_device);
4798 } else
4799 _scsih_determine_boot_device(ioc, raid_device, 1);
4800}
4801
4802/**
4803 * _scsih_sas_volume_delete - delete volume
4804 * @ioc: per adapter object
4805 * @element: IR config element data
4806 * Context: user.
4807 *
4808 * Return nothing.
4809 */
4810static void
4811_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4812 Mpi2EventIrConfigElement_t *element)
4813{
4814 struct _raid_device *raid_device;
4815 u16 handle = le16_to_cpu(element->VolDevHandle);
4816 unsigned long flags;
4817 struct MPT2SAS_TARGET *sas_target_priv_data;
4818
Eric Moore635374e2009-03-09 01:21:12 -06004819 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4820 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4821 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4822 if (!raid_device)
4823 return;
4824 if (raid_device->starget) {
4825 sas_target_priv_data = raid_device->starget->hostdata;
4826 sas_target_priv_data->deleted = 1;
4827 scsi_remove_target(&raid_device->starget->dev);
4828 }
4829 _scsih_raid_device_remove(ioc, raid_device);
4830}
4831
4832/**
4833 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4834 * @ioc: per adapter object
4835 * @element: IR config element data
4836 * Context: user.
4837 *
4838 * Return nothing.
4839 */
4840static void
4841_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4842 Mpi2EventIrConfigElement_t *element)
4843{
4844 struct _sas_device *sas_device;
4845 unsigned long flags;
4846 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4847
4848 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4849 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4850 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4851 if (!sas_device)
4852 return;
4853
4854 /* exposing raid component */
4855 sas_device->volume_handle = 0;
4856 sas_device->volume_wwid = 0;
4857 sas_device->hidden_raid_component = 0;
4858 _scsih_reprobe_target(sas_device->starget, 0);
4859}
4860
4861/**
4862 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4863 * @ioc: per adapter object
4864 * @element: IR config element data
4865 * Context: user.
4866 *
4867 * Return nothing.
4868 */
4869static void
4870_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4871 Mpi2EventIrConfigElement_t *element)
4872{
4873 struct _sas_device *sas_device;
4874 unsigned long flags;
4875 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4876
4877 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4878 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4879 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4880 if (!sas_device)
4881 return;
4882
4883 /* hiding raid component */
4884 mpt2sas_config_get_volume_handle(ioc, handle,
4885 &sas_device->volume_handle);
4886 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4887 &sas_device->volume_wwid);
4888 sas_device->hidden_raid_component = 1;
4889 _scsih_reprobe_target(sas_device->starget, 1);
4890}
4891
4892/**
4893 * _scsih_sas_pd_delete - delete pd component
4894 * @ioc: per adapter object
4895 * @element: IR config element data
4896 * Context: user.
4897 *
4898 * Return nothing.
4899 */
4900static void
4901_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4902 Mpi2EventIrConfigElement_t *element)
4903{
4904 struct _sas_device *sas_device;
4905 unsigned long flags;
4906 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4907
4908 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4909 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4910 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4911 if (!sas_device)
4912 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304913 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004914}
4915
4916/**
4917 * _scsih_sas_pd_add - remove pd component
4918 * @ioc: per adapter object
4919 * @element: IR config element data
4920 * Context: user.
4921 *
4922 * Return nothing.
4923 */
4924static void
4925_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4926 Mpi2EventIrConfigElement_t *element)
4927{
4928 struct _sas_device *sas_device;
4929 unsigned long flags;
4930 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304931 Mpi2ConfigReply_t mpi_reply;
4932 Mpi2SasDevicePage0_t sas_device_pg0;
4933 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304934 u64 sas_address;
4935 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06004936
4937 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4938 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4939 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304940 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004941 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304942 return;
4943 }
4944
4945 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4946 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4947 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4948 ioc->name, __FILE__, __LINE__, __func__);
4949 return;
4950 }
4951
4952 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4953 MPI2_IOCSTATUS_MASK;
4954 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4955 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4956 ioc->name, __FILE__, __LINE__, __func__);
4957 return;
4958 }
4959
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304960 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
4961 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
4962 mpt2sas_transport_update_links(ioc, sas_address, handle,
4963 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304964
4965 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06004966}
4967
4968#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4969/**
4970 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4971 * @ioc: per adapter object
4972 * @event_data: event data payload
4973 * Context: user.
4974 *
4975 * Return nothing.
4976 */
4977static void
4978_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4979 Mpi2EventDataIrConfigChangeList_t *event_data)
4980{
4981 Mpi2EventIrConfigElement_t *element;
4982 u8 element_type;
4983 int i;
4984 char *reason_str = NULL, *element_str = NULL;
4985
4986 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4987
4988 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4989 ioc->name, (le32_to_cpu(event_data->Flags) &
4990 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4991 "foreign" : "native", event_data->NumElements);
4992 for (i = 0; i < event_data->NumElements; i++, element++) {
4993 switch (element->ReasonCode) {
4994 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4995 reason_str = "add";
4996 break;
4997 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4998 reason_str = "remove";
4999 break;
5000 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5001 reason_str = "no change";
5002 break;
5003 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5004 reason_str = "hide";
5005 break;
5006 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5007 reason_str = "unhide";
5008 break;
5009 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5010 reason_str = "volume_created";
5011 break;
5012 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5013 reason_str = "volume_deleted";
5014 break;
5015 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5016 reason_str = "pd_created";
5017 break;
5018 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5019 reason_str = "pd_deleted";
5020 break;
5021 default:
5022 reason_str = "unknown reason";
5023 break;
5024 }
5025 element_type = le16_to_cpu(element->ElementFlags) &
5026 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5027 switch (element_type) {
5028 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5029 element_str = "volume";
5030 break;
5031 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5032 element_str = "phys disk";
5033 break;
5034 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5035 element_str = "hot spare";
5036 break;
5037 default:
5038 element_str = "unknown element";
5039 break;
5040 }
5041 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
5042 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5043 reason_str, le16_to_cpu(element->VolDevHandle),
5044 le16_to_cpu(element->PhysDiskDevHandle),
5045 element->PhysDiskNum);
5046 }
5047}
5048#endif
5049
5050/**
5051 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5052 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305053 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005054 * Context: user.
5055 *
5056 * Return nothing.
5057 */
5058static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305059_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5060 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005061{
5062 Mpi2EventIrConfigElement_t *element;
5063 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305064 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305065 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005066
5067#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5068 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5069 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5070
5071#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05305072 foreign_config = (le32_to_cpu(event_data->Flags) &
5073 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06005074
5075 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5076 for (i = 0; i < event_data->NumElements; i++, element++) {
5077
5078 switch (element->ReasonCode) {
5079 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5080 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305081 if (!foreign_config)
5082 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005083 break;
5084 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5085 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305086 if (!foreign_config)
5087 _scsih_sas_volume_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005088 break;
5089 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5090 _scsih_sas_pd_hide(ioc, element);
5091 break;
5092 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5093 _scsih_sas_pd_expose(ioc, element);
5094 break;
5095 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5096 _scsih_sas_pd_add(ioc, element);
5097 break;
5098 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5099 _scsih_sas_pd_delete(ioc, element);
5100 break;
5101 }
5102 }
5103}
5104
5105/**
5106 * _scsih_sas_ir_volume_event - IR volume event
5107 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305108 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005109 * Context: user.
5110 *
5111 * Return nothing.
5112 */
5113static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305114_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5115 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005116{
5117 u64 wwid;
5118 unsigned long flags;
5119 struct _raid_device *raid_device;
5120 u16 handle;
5121 u32 state;
5122 int rc;
5123 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305124 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005125
5126 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5127 return;
5128
5129 handle = le16_to_cpu(event_data->VolDevHandle);
5130 state = le32_to_cpu(event_data->NewValue);
5131 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
5132 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5133 le32_to_cpu(event_data->PreviousValue), state));
5134
5135 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5136 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5137 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5138
5139 switch (state) {
5140 case MPI2_RAID_VOL_STATE_MISSING:
5141 case MPI2_RAID_VOL_STATE_FAILED:
5142 if (!raid_device)
5143 break;
5144 if (raid_device->starget) {
5145 sas_target_priv_data = raid_device->starget->hostdata;
5146 sas_target_priv_data->deleted = 1;
5147 scsi_remove_target(&raid_device->starget->dev);
5148 }
5149 _scsih_raid_device_remove(ioc, raid_device);
5150 break;
5151
5152 case MPI2_RAID_VOL_STATE_ONLINE:
5153 case MPI2_RAID_VOL_STATE_DEGRADED:
5154 case MPI2_RAID_VOL_STATE_OPTIMAL:
5155 if (raid_device)
5156 break;
5157
5158 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5159 if (!wwid) {
5160 printk(MPT2SAS_ERR_FMT
5161 "failure at %s:%d/%s()!\n", ioc->name,
5162 __FILE__, __LINE__, __func__);
5163 break;
5164 }
5165
5166 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5167 if (!raid_device) {
5168 printk(MPT2SAS_ERR_FMT
5169 "failure at %s:%d/%s()!\n", ioc->name,
5170 __FILE__, __LINE__, __func__);
5171 break;
5172 }
5173
5174 raid_device->id = ioc->sas_id++;
5175 raid_device->channel = RAID_CHANNEL;
5176 raid_device->handle = handle;
5177 raid_device->wwid = wwid;
5178 _scsih_raid_device_add(ioc, raid_device);
5179 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5180 raid_device->id, 0);
5181 if (rc)
5182 _scsih_raid_device_remove(ioc, raid_device);
5183 break;
5184
5185 case MPI2_RAID_VOL_STATE_INITIALIZING:
5186 default:
5187 break;
5188 }
5189}
5190
5191/**
5192 * _scsih_sas_ir_physical_disk_event - PD event
5193 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305194 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005195 * Context: user.
5196 *
5197 * Return nothing.
5198 */
5199static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305200_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5201 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005202{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305203 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005204 u32 state;
5205 struct _sas_device *sas_device;
5206 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305207 Mpi2ConfigReply_t mpi_reply;
5208 Mpi2SasDevicePage0_t sas_device_pg0;
5209 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305210 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305211 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005212
5213 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5214 return;
5215
5216 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5217 state = le32_to_cpu(event_data->NewValue);
5218
5219 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
5220 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5221 le32_to_cpu(event_data->PreviousValue), state));
5222
5223 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5224 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5225 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5226
5227 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005228 case MPI2_RAID_PD_STATE_ONLINE:
5229 case MPI2_RAID_PD_STATE_DEGRADED:
5230 case MPI2_RAID_PD_STATE_REBUILDING:
5231 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305232 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06005233 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305234 return;
5235 }
5236
5237 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5238 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5239 handle))) {
5240 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5241 ioc->name, __FILE__, __LINE__, __func__);
5242 return;
5243 }
5244
5245 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5246 MPI2_IOCSTATUS_MASK;
5247 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5248 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5249 ioc->name, __FILE__, __LINE__, __func__);
5250 return;
5251 }
5252
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305253 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5254 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5255 mpt2sas_transport_update_links(ioc, sas_address, handle,
5256 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305257
5258 _scsih_add_device(ioc, handle, 0, 1);
5259
Eric Moore635374e2009-03-09 01:21:12 -06005260 break;
5261
Kashyap, Desai62727a72009-08-07 19:35:18 +05305262 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005263 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5264 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5265 case MPI2_RAID_PD_STATE_HOT_SPARE:
5266 default:
5267 break;
5268 }
5269}
5270
5271#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5272/**
5273 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5274 * @ioc: per adapter object
5275 * @event_data: event data payload
5276 * Context: user.
5277 *
5278 * Return nothing.
5279 */
5280static void
5281_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5282 Mpi2EventDataIrOperationStatus_t *event_data)
5283{
5284 char *reason_str = NULL;
5285
5286 switch (event_data->RAIDOperation) {
5287 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5288 reason_str = "resync";
5289 break;
5290 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5291 reason_str = "online capacity expansion";
5292 break;
5293 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5294 reason_str = "consistency check";
5295 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305296 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5297 reason_str = "background init";
5298 break;
5299 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5300 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005301 break;
5302 }
5303
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305304 if (!reason_str)
5305 return;
5306
Eric Moore635374e2009-03-09 01:21:12 -06005307 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5308 "\thandle(0x%04x), percent complete(%d)\n",
5309 ioc->name, reason_str,
5310 le16_to_cpu(event_data->VolDevHandle),
5311 event_data->PercentComplete);
5312}
5313#endif
5314
5315/**
5316 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5317 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305318 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005319 * Context: user.
5320 *
5321 * Return nothing.
5322 */
5323static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305324_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5325 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005326{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305327 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5328 static struct _raid_device *raid_device;
5329 unsigned long flags;
5330 u16 handle;
5331
Eric Moore635374e2009-03-09 01:21:12 -06005332#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5333 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305334 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305335 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005336#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305337
5338 /* code added for raid transport support */
5339 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5340
5341 handle = le16_to_cpu(event_data->VolDevHandle);
5342
5343 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5344 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5345 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5346
5347 if (!raid_device)
5348 return;
5349
5350 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5351 raid_device->percent_complete =
5352 event_data->PercentComplete;
5353 }
Eric Moore635374e2009-03-09 01:21:12 -06005354}
5355
5356/**
5357 * _scsih_task_set_full - handle task set full
5358 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305359 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005360 * Context: user.
5361 *
5362 * Throttle back qdepth.
5363 */
5364static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305365_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5366 *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005367{
5368 unsigned long flags;
5369 struct _sas_device *sas_device;
5370 static struct _raid_device *raid_device;
5371 struct scsi_device *sdev;
5372 int depth;
5373 u16 current_depth;
5374 u16 handle;
5375 int id, channel;
5376 u64 sas_address;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305377 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005378
5379 current_depth = le16_to_cpu(event_data->CurrentDepth);
5380 handle = le16_to_cpu(event_data->DevHandle);
5381 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5382 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5383 if (!sas_device) {
5384 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5385 return;
5386 }
5387 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5388 id = sas_device->id;
5389 channel = sas_device->channel;
5390 sas_address = sas_device->sas_address;
5391
5392 /* if hidden raid component, then change to volume characteristics */
5393 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
5394 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5395 raid_device = _scsih_raid_device_find_by_handle(
5396 ioc, sas_device->volume_handle);
5397 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5398 if (raid_device) {
5399 id = raid_device->id;
5400 channel = raid_device->channel;
5401 handle = raid_device->handle;
5402 sas_address = raid_device->wwid;
5403 }
5404 }
5405
5406 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5407 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
5408 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5409 handle, (unsigned long long)sas_address, current_depth);
5410
5411 shost_for_each_device(sdev, ioc->shost) {
5412 if (sdev->id == id && sdev->channel == channel) {
5413 if (current_depth > sdev->queue_depth) {
5414 if (ioc->logging_level &
5415 MPT_DEBUG_TASK_SET_FULL)
5416 sdev_printk(KERN_INFO, sdev, "strange "
5417 "observation, the queue depth is"
5418 " (%d) meanwhile fw queue depth "
5419 "is (%d)\n", sdev->queue_depth,
5420 current_depth);
5421 continue;
5422 }
5423 depth = scsi_track_queue_full(sdev,
5424 current_depth - 1);
5425 if (depth > 0)
5426 sdev_printk(KERN_INFO, sdev, "Queue depth "
5427 "reduced to (%d)\n", depth);
5428 else if (depth < 0)
5429 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5430 "Queueing is being disabled\n");
5431 else if (depth == 0)
5432 if (ioc->logging_level &
5433 MPT_DEBUG_TASK_SET_FULL)
5434 sdev_printk(KERN_INFO, sdev,
5435 "Queue depth not changed yet\n");
5436 }
5437 }
5438}
5439
5440/**
5441 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5442 * @ioc: per adapter object
5443 * @sas_address: sas address
5444 * @slot: enclosure slot id
5445 * @handle: device handle
5446 *
5447 * After host reset, find out whether devices are still responding.
5448 * Used in _scsi_remove_unresponsive_sas_devices.
5449 *
5450 * Return nothing.
5451 */
5452static void
5453_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5454 u16 slot, u16 handle)
5455{
5456 struct MPT2SAS_TARGET *sas_target_priv_data;
5457 struct scsi_target *starget;
5458 struct _sas_device *sas_device;
5459 unsigned long flags;
5460
5461 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5462 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5463 if (sas_device->sas_address == sas_address &&
5464 sas_device->slot == slot && sas_device->starget) {
5465 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305466 sas_device->state = 0;
5467 starget = sas_device->starget;
5468 sas_target_priv_data = starget->hostdata;
5469 sas_target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005470 starget_printk(KERN_INFO, sas_device->starget,
5471 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5472 "logical id(0x%016llx), slot(%d)\n", handle,
5473 (unsigned long long)sas_device->sas_address,
5474 (unsigned long long)
5475 sas_device->enclosure_logical_id,
5476 sas_device->slot);
5477 if (sas_device->handle == handle)
5478 goto out;
5479 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5480 sas_device->handle);
5481 sas_device->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005482 sas_target_priv_data->handle = handle;
5483 goto out;
5484 }
5485 }
5486 out:
5487 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5488}
5489
5490/**
5491 * _scsih_search_responding_sas_devices -
5492 * @ioc: per adapter object
5493 *
5494 * After host reset, find out whether devices are still responding.
5495 * If not remove.
5496 *
5497 * Return nothing.
5498 */
5499static void
5500_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5501{
5502 Mpi2SasDevicePage0_t sas_device_pg0;
5503 Mpi2ConfigReply_t mpi_reply;
5504 u16 ioc_status;
5505 __le64 sas_address;
5506 u16 handle;
5507 u32 device_info;
5508 u16 slot;
5509
5510 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5511
5512 if (list_empty(&ioc->sas_device_list))
5513 return;
5514
5515 handle = 0xFFFF;
5516 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5517 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5518 handle))) {
5519 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5520 MPI2_IOCSTATUS_MASK;
5521 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5522 break;
5523 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5524 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5525 if (!(_scsih_is_end_device(device_info)))
5526 continue;
5527 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5528 slot = le16_to_cpu(sas_device_pg0.Slot);
5529 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5530 handle);
5531 }
5532}
5533
5534/**
5535 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5536 * @ioc: per adapter object
5537 * @wwid: world wide identifier for raid volume
5538 * @handle: device handle
5539 *
5540 * After host reset, find out whether devices are still responding.
5541 * Used in _scsi_remove_unresponsive_raid_devices.
5542 *
5543 * Return nothing.
5544 */
5545static void
5546_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5547 u16 handle)
5548{
5549 struct MPT2SAS_TARGET *sas_target_priv_data;
5550 struct scsi_target *starget;
5551 struct _raid_device *raid_device;
5552 unsigned long flags;
5553
5554 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5555 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5556 if (raid_device->wwid == wwid && raid_device->starget) {
5557 raid_device->responding = 1;
5558 starget_printk(KERN_INFO, raid_device->starget,
5559 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5560 (unsigned long long)raid_device->wwid);
5561 if (raid_device->handle == handle)
5562 goto out;
5563 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5564 raid_device->handle);
5565 raid_device->handle = handle;
5566 starget = raid_device->starget;
5567 sas_target_priv_data = starget->hostdata;
5568 sas_target_priv_data->handle = handle;
5569 goto out;
5570 }
5571 }
5572 out:
5573 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5574}
5575
5576/**
5577 * _scsih_search_responding_raid_devices -
5578 * @ioc: per adapter object
5579 *
5580 * After host reset, find out whether devices are still responding.
5581 * If not remove.
5582 *
5583 * Return nothing.
5584 */
5585static void
5586_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5587{
5588 Mpi2RaidVolPage1_t volume_pg1;
5589 Mpi2ConfigReply_t mpi_reply;
5590 u16 ioc_status;
5591 u16 handle;
5592
5593 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5594
5595 if (list_empty(&ioc->raid_device_list))
5596 return;
5597
5598 handle = 0xFFFF;
5599 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5600 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5601 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5602 MPI2_IOCSTATUS_MASK;
5603 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5604 break;
5605 handle = le16_to_cpu(volume_pg1.DevHandle);
5606 _scsih_mark_responding_raid_device(ioc,
5607 le64_to_cpu(volume_pg1.WWID), handle);
5608 }
5609}
5610
5611/**
5612 * _scsih_mark_responding_expander - mark a expander as responding
5613 * @ioc: per adapter object
5614 * @sas_address: sas address
5615 * @handle:
5616 *
5617 * After host reset, find out whether devices are still responding.
5618 * Used in _scsi_remove_unresponsive_expanders.
5619 *
5620 * Return nothing.
5621 */
5622static void
5623_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5624 u16 handle)
5625{
5626 struct _sas_node *sas_expander;
5627 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305628 int i;
Eric Moore635374e2009-03-09 01:21:12 -06005629
5630 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5631 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305632 if (sas_expander->sas_address != sas_address)
5633 continue;
5634 sas_expander->responding = 1;
5635 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06005636 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305637 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
5638 " from(0x%04x) to (0x%04x)!!!\n",
5639 (unsigned long long)sas_expander->sas_address,
5640 sas_expander->handle, handle);
5641 sas_expander->handle = handle;
5642 for (i = 0 ; i < sas_expander->num_phys ; i++)
5643 sas_expander->phy[i].handle = handle;
5644 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005645 }
5646 out:
5647 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5648}
5649
5650/**
5651 * _scsih_search_responding_expanders -
5652 * @ioc: per adapter object
5653 *
5654 * After host reset, find out whether devices are still responding.
5655 * If not remove.
5656 *
5657 * Return nothing.
5658 */
5659static void
5660_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5661{
5662 Mpi2ExpanderPage0_t expander_pg0;
5663 Mpi2ConfigReply_t mpi_reply;
5664 u16 ioc_status;
5665 __le64 sas_address;
5666 u16 handle;
5667
5668 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5669
5670 if (list_empty(&ioc->sas_expander_list))
5671 return;
5672
5673 handle = 0xFFFF;
5674 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5675 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5676
5677 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5678 MPI2_IOCSTATUS_MASK;
5679 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5680 break;
5681
5682 handle = le16_to_cpu(expander_pg0.DevHandle);
5683 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5684 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5685 "sas_addr(0x%016llx)\n", handle,
5686 (unsigned long long)sas_address);
5687 _scsih_mark_responding_expander(ioc, sas_address, handle);
5688 }
5689
5690}
5691
5692/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305693 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06005694 * @ioc: per adapter object
5695 *
5696 * Return nothing.
5697 */
5698static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305699_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06005700{
5701 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305702 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06005703 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06005704
Eric Moore635374e2009-03-09 01:21:12 -06005705
5706 list_for_each_entry_safe(sas_device, sas_device_next,
5707 &ioc->sas_device_list, list) {
5708 if (sas_device->responding) {
5709 sas_device->responding = 0;
5710 continue;
5711 }
5712 if (sas_device->starget)
5713 starget_printk(KERN_INFO, sas_device->starget,
5714 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5715 "enclosure logical id(0x%016llx), slot(%d)\n",
5716 sas_device->handle,
5717 (unsigned long long)sas_device->sas_address,
5718 (unsigned long long)
5719 sas_device->enclosure_logical_id,
5720 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305721 /* invalidate the device handle */
5722 sas_device->handle = 0;
5723 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005724 }
5725
5726 list_for_each_entry_safe(raid_device, raid_device_next,
5727 &ioc->raid_device_list, list) {
5728 if (raid_device->responding) {
5729 raid_device->responding = 0;
5730 continue;
5731 }
5732 if (raid_device->starget) {
5733 starget_printk(KERN_INFO, raid_device->starget,
5734 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5735 raid_device->handle,
5736 (unsigned long long)raid_device->wwid);
5737 scsi_remove_target(&raid_device->starget->dev);
5738 }
5739 _scsih_raid_device_remove(ioc, raid_device);
5740 }
5741
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305742 retry_expander_search:
5743 sas_expander = NULL;
5744 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06005745 if (sas_expander->responding) {
5746 sas_expander->responding = 0;
5747 continue;
5748 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305749 _scsih_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305750 goto retry_expander_search;
5751 }
5752}
5753
5754/**
5755 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5756 * @ioc: per adapter object
5757 * @reset_phase: phase
5758 *
5759 * The handler for doing any required cleanup or initialization.
5760 *
5761 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5762 * MPT2_IOC_DONE_RESET
5763 *
5764 * Return nothing.
5765 */
5766void
5767mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5768{
5769 switch (reset_phase) {
5770 case MPT2_IOC_PRE_RESET:
5771 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5772 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305773 break;
5774 case MPT2_IOC_AFTER_RESET:
5775 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5776 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305777 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
5778 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
5779 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
5780 complete(&ioc->scsih_cmds.done);
5781 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305782 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5783 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5784 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5785 complete(&ioc->tm_cmds.done);
5786 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305787 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305788 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305789 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305790 break;
5791 case MPT2_IOC_DONE_RESET:
5792 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5793 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305794 _scsih_sas_host_refresh(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305795 break;
Eric Moore635374e2009-03-09 01:21:12 -06005796 }
5797}
5798
5799/**
5800 * _firmware_event_work - delayed task for processing firmware events
5801 * @ioc: per adapter object
5802 * @work: equal to the fw_event_work object
5803 * Context: user.
5804 *
5805 * Return nothing.
5806 */
5807static void
5808_firmware_event_work(struct work_struct *work)
5809{
5810 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305811 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06005812 unsigned long flags;
5813 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5814
Eric Moore635374e2009-03-09 01:21:12 -06005815 /* the queue is being flushed so ignore this event */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305816 if (ioc->remove_host || fw_event->cancel_pending_work) {
Eric Moore635374e2009-03-09 01:21:12 -06005817 _scsih_fw_event_free(ioc, fw_event);
5818 return;
5819 }
Eric Moore635374e2009-03-09 01:21:12 -06005820
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305821 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
5822 _scsih_fw_event_free(ioc, fw_event);
5823 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5824 if (ioc->shost_recovery) {
5825 init_completion(&ioc->shost_recovery_done);
5826 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
5827 flags);
5828 wait_for_completion(&ioc->shost_recovery_done);
5829 } else
5830 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
5831 flags);
5832 _scsih_search_responding_sas_devices(ioc);
5833 _scsih_search_responding_raid_devices(ioc);
5834 _scsih_search_responding_expanders(ioc);
5835 _scsih_remove_unresponding_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005836 return;
5837 }
Eric Moore635374e2009-03-09 01:21:12 -06005838
5839 switch (fw_event->event) {
5840 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305841 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005842 break;
5843 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305844 _scsih_sas_device_status_change_event(ioc,
5845 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005846 break;
5847 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305848 _scsih_sas_discovery_event(ioc,
5849 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005850 break;
5851 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305852 _scsih_sas_broadcast_primative_event(ioc,
5853 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005854 break;
5855 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5856 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305857 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005858 break;
5859 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305860 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005861 break;
5862 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305863 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005864 break;
5865 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305866 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005867 break;
5868 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305869 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005870 break;
5871 case MPI2_EVENT_TASK_SET_FULL:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305872 _scsih_task_set_full(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005873 break;
5874 }
5875 _scsih_fw_event_free(ioc, fw_event);
5876}
5877
5878/**
5879 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5880 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305881 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06005882 * @reply: reply message frame(lower 32bit addr)
5883 * Context: interrupt.
5884 *
5885 * This function merely adds a new work task into ioc->firmware_event_thread.
5886 * The tasks are worked from _firmware_event_work in user context.
5887 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305888 * Return 1 meaning mf should be freed from _base_interrupt
5889 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06005890 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305891u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305892mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
5893 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06005894{
5895 struct fw_event_work *fw_event;
5896 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06005897 u16 event;
5898
5899 /* events turned off due to host reset or driver unloading */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305900 if (ioc->remove_host)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305901 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005902
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305903 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06005904 event = le16_to_cpu(mpi_reply->Event);
5905
5906 switch (event) {
5907 /* handle these */
5908 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5909 {
5910 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5911 (Mpi2EventDataSasBroadcastPrimitive_t *)
5912 mpi_reply->EventData;
5913
5914 if (baen_data->Primitive !=
5915 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5916 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305917 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005918 ioc->broadcast_aen_busy = 1;
5919 break;
5920 }
5921
5922 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5923 _scsih_check_topo_delete_events(ioc,
5924 (Mpi2EventDataSasTopologyChangeList_t *)
5925 mpi_reply->EventData);
5926 break;
5927
5928 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5929 case MPI2_EVENT_IR_OPERATION_STATUS:
5930 case MPI2_EVENT_SAS_DISCOVERY:
5931 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5932 case MPI2_EVENT_IR_VOLUME:
5933 case MPI2_EVENT_IR_PHYSICAL_DISK:
5934 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5935 case MPI2_EVENT_TASK_SET_FULL:
5936 break;
5937
5938 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305939 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005940 }
5941
5942 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5943 if (!fw_event) {
5944 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5945 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305946 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005947 }
5948 fw_event->event_data =
5949 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5950 if (!fw_event->event_data) {
5951 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5952 ioc->name, __FILE__, __LINE__, __func__);
5953 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305954 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005955 }
5956
5957 memcpy(fw_event->event_data, mpi_reply->EventData,
5958 mpi_reply->EventDataLength*4);
5959 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305960 fw_event->VF_ID = mpi_reply->VF_ID;
5961 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06005962 fw_event->event = event;
5963 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305964 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005965}
5966
5967/* shost template */
5968static struct scsi_host_template scsih_driver_template = {
5969 .module = THIS_MODULE,
5970 .name = "Fusion MPT SAS Host",
5971 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06005972 .queuecommand = _scsih_qcmd,
5973 .target_alloc = _scsih_target_alloc,
5974 .slave_alloc = _scsih_slave_alloc,
5975 .slave_configure = _scsih_slave_configure,
5976 .target_destroy = _scsih_target_destroy,
5977 .slave_destroy = _scsih_slave_destroy,
5978 .change_queue_depth = _scsih_change_queue_depth,
5979 .change_queue_type = _scsih_change_queue_type,
5980 .eh_abort_handler = _scsih_abort,
5981 .eh_device_reset_handler = _scsih_dev_reset,
5982 .eh_target_reset_handler = _scsih_target_reset,
5983 .eh_host_reset_handler = _scsih_host_reset,
5984 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06005985 .can_queue = 1,
5986 .this_id = -1,
5987 .sg_tablesize = MPT2SAS_SG_DEPTH,
5988 .max_sectors = 8192,
5989 .cmd_per_lun = 7,
5990 .use_clustering = ENABLE_CLUSTERING,
5991 .shost_attrs = mpt2sas_host_attrs,
5992 .sdev_attrs = mpt2sas_dev_attrs,
5993};
5994
5995/**
5996 * _scsih_expander_node_remove - removing expander device from list.
5997 * @ioc: per adapter object
5998 * @sas_expander: the sas_device object
5999 * Context: Calling function should acquire ioc->sas_node_lock.
6000 *
6001 * Removing object and freeing associated memory from the
6002 * ioc->sas_expander_list.
6003 *
6004 * Return nothing.
6005 */
6006static void
6007_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6008 struct _sas_node *sas_expander)
6009{
6010 struct _sas_port *mpt2sas_port;
6011 struct _sas_device *sas_device;
6012 struct _sas_node *expander_sibling;
6013 unsigned long flags;
6014
6015 if (!sas_expander)
6016 return;
6017
6018 /* remove sibling ports attached to this expander */
6019 retry_device_search:
6020 list_for_each_entry(mpt2sas_port,
6021 &sas_expander->sas_port_list, port_list) {
6022 if (mpt2sas_port->remote_identify.device_type ==
6023 SAS_END_DEVICE) {
6024 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6025 sas_device =
6026 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6027 mpt2sas_port->remote_identify.sas_address);
6028 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6029 if (!sas_device)
6030 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306031 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05306032 if (ioc->shost_recovery)
6033 return;
Eric Moore635374e2009-03-09 01:21:12 -06006034 goto retry_device_search;
6035 }
6036 }
6037
6038 retry_expander_search:
6039 list_for_each_entry(mpt2sas_port,
6040 &sas_expander->sas_port_list, port_list) {
6041
6042 if (mpt2sas_port->remote_identify.device_type ==
6043 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
6044 mpt2sas_port->remote_identify.device_type ==
6045 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
6046
6047 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6048 expander_sibling =
6049 mpt2sas_scsih_expander_find_by_sas_address(
6050 ioc, mpt2sas_port->remote_identify.sas_address);
6051 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6052 if (!expander_sibling)
6053 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306054 _scsih_expander_remove(ioc,
6055 expander_sibling->sas_address);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05306056 if (ioc->shost_recovery)
6057 return;
Eric Moore635374e2009-03-09 01:21:12 -06006058 goto retry_expander_search;
6059 }
6060 }
6061
6062 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306063 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006064
6065 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6066 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6067 sas_expander->handle, (unsigned long long)
6068 sas_expander->sas_address);
6069
6070 list_del(&sas_expander->list);
6071 kfree(sas_expander->phy);
6072 kfree(sas_expander);
6073}
6074
6075/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05306076 * _scsih_ir_shutdown - IR shutdown notification
6077 * @ioc: per adapter object
6078 *
6079 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6080 * the host system is shutting down.
6081 *
6082 * Return nothing.
6083 */
6084static void
6085_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6086{
6087 Mpi2RaidActionRequest_t *mpi_request;
6088 Mpi2RaidActionReply_t *mpi_reply;
6089 u16 smid;
6090
6091 /* is IR firmware build loaded ? */
6092 if (!ioc->ir_firmware)
6093 return;
6094
6095 /* are there any volumes ? */
6096 if (list_empty(&ioc->raid_device_list))
6097 return;
6098
6099 mutex_lock(&ioc->scsih_cmds.mutex);
6100
6101 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6102 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6103 ioc->name, __func__);
6104 goto out;
6105 }
6106 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6107
6108 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6109 if (!smid) {
6110 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6111 ioc->name, __func__);
6112 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6113 goto out;
6114 }
6115
6116 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6117 ioc->scsih_cmds.smid = smid;
6118 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6119
6120 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6121 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6122
6123 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6124 init_completion(&ioc->scsih_cmds.done);
6125 mpt2sas_base_put_smid_default(ioc, smid);
6126 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6127
6128 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6129 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6130 ioc->name, __func__);
6131 goto out;
6132 }
6133
6134 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6135 mpi_reply = ioc->scsih_cmds.reply;
6136
6137 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6138 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6139 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6140 le32_to_cpu(mpi_reply->IOCLogInfo));
6141 }
6142
6143 out:
6144 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6145 mutex_unlock(&ioc->scsih_cmds.mutex);
6146}
6147
6148/**
6149 * _scsih_shutdown - routine call during system shutdown
6150 * @pdev: PCI device struct
6151 *
6152 * Return nothing.
6153 */
6154static void
6155_scsih_shutdown(struct pci_dev *pdev)
6156{
6157 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6158 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306159 struct workqueue_struct *wq;
6160 unsigned long flags;
6161
6162 ioc->remove_host = 1;
6163 _scsih_fw_event_cleanup_queue(ioc);
6164
6165 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6166 wq = ioc->firmware_event_thread;
6167 ioc->firmware_event_thread = NULL;
6168 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6169 if (wq)
6170 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306171
6172 _scsih_ir_shutdown(ioc);
6173 mpt2sas_base_detach(ioc);
6174}
6175
6176/**
Eric Moored5d135b2009-05-18 13:02:08 -06006177 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06006178 * @pdev: PCI device struct
6179 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05306180 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06006181 * Return nothing.
6182 */
6183static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06006184_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006185{
6186 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6187 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6188 struct _sas_port *mpt2sas_port;
6189 struct _sas_device *sas_device;
6190 struct _sas_node *expander_sibling;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306191 struct _raid_device *raid_device, *next;
6192 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006193 struct workqueue_struct *wq;
6194 unsigned long flags;
6195
6196 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306197 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006198
6199 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6200 wq = ioc->firmware_event_thread;
6201 ioc->firmware_event_thread = NULL;
6202 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6203 if (wq)
6204 destroy_workqueue(wq);
6205
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306206 /* release all the volumes */
6207 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6208 list) {
6209 if (raid_device->starget) {
6210 sas_target_priv_data =
6211 raid_device->starget->hostdata;
6212 sas_target_priv_data->deleted = 1;
6213 scsi_remove_target(&raid_device->starget->dev);
6214 }
6215 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6216 "(0x%016llx)\n", ioc->name, raid_device->handle,
6217 (unsigned long long) raid_device->wwid);
6218 _scsih_raid_device_remove(ioc, raid_device);
6219 }
6220
Eric Moore635374e2009-03-09 01:21:12 -06006221 /* free ports attached to the sas_host */
6222 retry_again:
6223 list_for_each_entry(mpt2sas_port,
6224 &ioc->sas_hba.sas_port_list, port_list) {
6225 if (mpt2sas_port->remote_identify.device_type ==
6226 SAS_END_DEVICE) {
6227 sas_device =
6228 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6229 mpt2sas_port->remote_identify.sas_address);
6230 if (sas_device) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306231 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006232 goto retry_again;
6233 }
6234 } else {
6235 expander_sibling =
6236 mpt2sas_scsih_expander_find_by_sas_address(ioc,
6237 mpt2sas_port->remote_identify.sas_address);
6238 if (expander_sibling) {
6239 _scsih_expander_remove(ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306240 expander_sibling->sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006241 goto retry_again;
6242 }
6243 }
6244 }
6245
6246 /* free phys attached to the sas_host */
6247 if (ioc->sas_hba.num_phys) {
6248 kfree(ioc->sas_hba.phy);
6249 ioc->sas_hba.phy = NULL;
6250 ioc->sas_hba.num_phys = 0;
6251 }
6252
6253 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306254 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06006255 list_del(&ioc->list);
6256 scsi_remove_host(shost);
6257 scsi_host_put(shost);
6258}
6259
6260/**
6261 * _scsih_probe_boot_devices - reports 1st device
6262 * @ioc: per adapter object
6263 *
6264 * If specified in bios page 2, this routine reports the 1st
6265 * device scsi-ml or sas transport for persistent boot device
6266 * purposes. Please refer to function _scsih_determine_boot_device()
6267 */
6268static void
6269_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6270{
6271 u8 is_raid;
6272 void *device;
6273 struct _sas_device *sas_device;
6274 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306275 u16 handle;
6276 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006277 u64 sas_address;
6278 unsigned long flags;
6279 int rc;
6280
6281 device = NULL;
6282 if (ioc->req_boot_device.device) {
6283 device = ioc->req_boot_device.device;
6284 is_raid = ioc->req_boot_device.is_raid;
6285 } else if (ioc->req_alt_boot_device.device) {
6286 device = ioc->req_alt_boot_device.device;
6287 is_raid = ioc->req_alt_boot_device.is_raid;
6288 } else if (ioc->current_boot_device.device) {
6289 device = ioc->current_boot_device.device;
6290 is_raid = ioc->current_boot_device.is_raid;
6291 }
6292
6293 if (!device)
6294 return;
6295
6296 if (is_raid) {
6297 raid_device = device;
6298 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6299 raid_device->id, 0);
6300 if (rc)
6301 _scsih_raid_device_remove(ioc, raid_device);
6302 } else {
6303 sas_device = device;
6304 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306305 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006306 sas_address = sas_device->sas_address;
6307 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6308 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6309 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6310 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306311 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006312 _scsih_sas_device_remove(ioc, sas_device);
6313 } else if (!sas_device->starget) {
6314 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306315 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006316 _scsih_sas_device_remove(ioc, sas_device);
6317 }
6318 }
6319}
6320
6321/**
6322 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6323 * @ioc: per adapter object
6324 *
6325 * Called during initial loading of the driver.
6326 */
6327static void
6328_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6329{
6330 struct _raid_device *raid_device, *raid_next;
6331 int rc;
6332
6333 list_for_each_entry_safe(raid_device, raid_next,
6334 &ioc->raid_device_list, list) {
6335 if (raid_device->starget)
6336 continue;
6337 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6338 raid_device->id, 0);
6339 if (rc)
6340 _scsih_raid_device_remove(ioc, raid_device);
6341 }
6342}
6343
6344/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306345 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06006346 * @ioc: per adapter object
6347 *
6348 * Called during initial loading of the driver.
6349 */
6350static void
6351_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6352{
6353 struct _sas_device *sas_device, *next;
6354 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06006355
6356 /* SAS Device List */
6357 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6358 list) {
6359 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6360 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6361 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6362
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306363 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6364 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006365 _scsih_sas_device_remove(ioc, sas_device);
6366 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306367 mpt2sas_transport_port_remove(ioc,
6368 sas_device->sas_address,
6369 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006370 _scsih_sas_device_remove(ioc, sas_device);
6371 }
6372 }
6373}
6374
6375/**
6376 * _scsih_probe_devices - probing for devices
6377 * @ioc: per adapter object
6378 *
6379 * Called during initial loading of the driver.
6380 */
6381static void
6382_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6383{
6384 u16 volume_mapping_flags =
6385 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6386 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6387
6388 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6389 return; /* return when IOC doesn't support initiator mode */
6390
6391 _scsih_probe_boot_devices(ioc);
6392
6393 if (ioc->ir_firmware) {
6394 if ((volume_mapping_flags &
6395 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6396 _scsih_probe_sas(ioc);
6397 _scsih_probe_raid(ioc);
6398 } else {
6399 _scsih_probe_raid(ioc);
6400 _scsih_probe_sas(ioc);
6401 }
6402 } else
6403 _scsih_probe_sas(ioc);
6404}
6405
6406/**
Eric Moored5d135b2009-05-18 13:02:08 -06006407 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006408 * @pdev: PCI device struct
6409 * @id: pci device id
6410 *
6411 * Returns 0 success, anything else error.
6412 */
6413static int
Eric Moored5d135b2009-05-18 13:02:08 -06006414_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006415{
6416 struct MPT2SAS_ADAPTER *ioc;
6417 struct Scsi_Host *shost;
6418
6419 shost = scsi_host_alloc(&scsih_driver_template,
6420 sizeof(struct MPT2SAS_ADAPTER));
6421 if (!shost)
6422 return -ENODEV;
6423
6424 /* init local params */
6425 ioc = shost_priv(shost);
6426 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6427 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006428 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006429 ioc->shost = shost;
6430 ioc->id = mpt_ids++;
6431 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6432 ioc->pdev = pdev;
6433 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6434 ioc->tm_cb_idx = tm_cb_idx;
6435 ioc->ctl_cb_idx = ctl_cb_idx;
6436 ioc->base_cb_idx = base_cb_idx;
6437 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05306438 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006439 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306440 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6441 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006442 ioc->logging_level = logging_level;
6443 /* misc semaphores and spin locks */
6444 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6445 spin_lock_init(&ioc->scsi_lookup_lock);
6446 spin_lock_init(&ioc->sas_device_lock);
6447 spin_lock_init(&ioc->sas_node_lock);
6448 spin_lock_init(&ioc->fw_event_lock);
6449 spin_lock_init(&ioc->raid_device_lock);
6450
6451 INIT_LIST_HEAD(&ioc->sas_device_list);
6452 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6453 INIT_LIST_HEAD(&ioc->sas_expander_list);
6454 INIT_LIST_HEAD(&ioc->fw_event_list);
6455 INIT_LIST_HEAD(&ioc->raid_device_list);
6456 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306457 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Eric Moore635374e2009-03-09 01:21:12 -06006458
6459 /* init shost parameters */
6460 shost->max_cmd_len = 16;
6461 shost->max_lun = max_lun;
6462 shost->transportt = mpt2sas_transport_template;
6463 shost->unique_id = ioc->id;
6464
6465 if ((scsi_add_host(shost, &pdev->dev))) {
6466 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6467 ioc->name, __FILE__, __LINE__, __func__);
6468 list_del(&ioc->list);
6469 goto out_add_shost_fail;
6470 }
6471
Eric Moore3c621b32009-05-18 12:59:41 -06006472 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6473 | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306474 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006475
Eric Moore635374e2009-03-09 01:21:12 -06006476 /* event thread */
6477 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6478 "fw_event%d", ioc->id);
6479 ioc->firmware_event_thread = create_singlethread_workqueue(
6480 ioc->firmware_event_name);
6481 if (!ioc->firmware_event_thread) {
6482 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6483 ioc->name, __FILE__, __LINE__, __func__);
6484 goto out_thread_fail;
6485 }
6486
6487 ioc->wait_for_port_enable_to_complete = 1;
6488 if ((mpt2sas_base_attach(ioc))) {
6489 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6490 ioc->name, __FILE__, __LINE__, __func__);
6491 goto out_attach_fail;
6492 }
6493
6494 ioc->wait_for_port_enable_to_complete = 0;
6495 _scsih_probe_devices(ioc);
6496 return 0;
6497
6498 out_attach_fail:
6499 destroy_workqueue(ioc->firmware_event_thread);
6500 out_thread_fail:
6501 list_del(&ioc->list);
6502 scsi_remove_host(shost);
6503 out_add_shost_fail:
6504 return -ENODEV;
6505}
6506
6507#ifdef CONFIG_PM
6508/**
Eric Moored5d135b2009-05-18 13:02:08 -06006509 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006510 * @pdev: PCI device struct
6511 * @state: PM state change to (usually PCI_D3)
6512 *
6513 * Returns 0 success, anything else error.
6514 */
6515static int
Eric Moored5d135b2009-05-18 13:02:08 -06006516_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006517{
6518 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6519 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6520 u32 device_state;
6521
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306522 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006523 flush_scheduled_work();
6524 scsi_block_requests(shost);
6525 device_state = pci_choose_state(pdev, state);
6526 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6527 "operating state [D%d]\n", ioc->name, pdev,
6528 pci_name(pdev), device_state);
6529
6530 mpt2sas_base_free_resources(ioc);
6531 pci_save_state(pdev);
6532 pci_disable_device(pdev);
6533 pci_set_power_state(pdev, device_state);
6534 return 0;
6535}
6536
6537/**
Eric Moored5d135b2009-05-18 13:02:08 -06006538 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006539 * @pdev: PCI device struct
6540 *
6541 * Returns 0 success, anything else error.
6542 */
6543static int
Eric Moored5d135b2009-05-18 13:02:08 -06006544_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006545{
6546 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6547 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6548 u32 device_state = pdev->current_state;
6549 int r;
6550
6551 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6552 "operating state [D%d]\n", ioc->name, pdev,
6553 pci_name(pdev), device_state);
6554
6555 pci_set_power_state(pdev, PCI_D0);
6556 pci_enable_wake(pdev, PCI_D0, 0);
6557 pci_restore_state(pdev);
6558 ioc->pdev = pdev;
6559 r = mpt2sas_base_map_resources(ioc);
6560 if (r)
6561 return r;
6562
6563 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6564 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306565 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006566 return 0;
6567}
6568#endif /* CONFIG_PM */
6569
6570
6571static struct pci_driver scsih_driver = {
6572 .name = MPT2SAS_DRIVER_NAME,
6573 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06006574 .probe = _scsih_probe,
6575 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05306576 .shutdown = _scsih_shutdown,
Eric Moore635374e2009-03-09 01:21:12 -06006577#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06006578 .suspend = _scsih_suspend,
6579 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06006580#endif
6581};
6582
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306583/* raid transport support */
6584static struct raid_function_template mpt2sas_raid_functions = {
6585 .cookie = &scsih_driver_template,
6586 .is_raid = _scsih_is_raid,
6587 .get_resync = _scsih_get_resync,
6588 .get_state = _scsih_get_state,
6589};
Eric Moore635374e2009-03-09 01:21:12 -06006590
6591/**
Eric Moored5d135b2009-05-18 13:02:08 -06006592 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06006593 *
6594 * Returns 0 success, anything else error.
6595 */
6596static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06006597_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06006598{
6599 int error;
6600
6601 mpt_ids = 0;
6602 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
6603 MPT2SAS_DRIVER_VERSION);
6604
6605 mpt2sas_transport_template =
6606 sas_attach_transport(&mpt2sas_transport_functions);
6607 if (!mpt2sas_transport_template)
6608 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306609 /* raid transport support */
6610 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
6611 if (!mpt2sas_raid_template) {
6612 sas_release_transport(mpt2sas_transport_template);
6613 return -ENODEV;
6614 }
Eric Moore635374e2009-03-09 01:21:12 -06006615
6616 mpt2sas_base_initialize_callback_handler();
6617
6618 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06006619 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06006620
6621 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06006622 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06006623
6624 /* base internal commands callback handler */
6625 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
6626
6627 /* transport internal commands callback handler */
6628 transport_cb_idx = mpt2sas_base_register_callback_handler(
6629 mpt2sas_transport_done);
6630
Kashyap, Desai744090d2009-10-05 15:56:56 +05306631 /* scsih internal commands callback handler */
6632 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
6633
Eric Moore635374e2009-03-09 01:21:12 -06006634 /* configuration page API internal commands callback handler */
6635 config_cb_idx = mpt2sas_base_register_callback_handler(
6636 mpt2sas_config_done);
6637
6638 /* ctl module callback handler */
6639 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
6640
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306641 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
6642 _scsih_tm_tr_complete);
6643 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
6644 _scsih_sas_control_complete);
6645
Eric Moore635374e2009-03-09 01:21:12 -06006646 mpt2sas_ctl_init();
6647
6648 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306649 if (error) {
6650 /* raid transport support */
6651 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06006652 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306653 }
Eric Moore635374e2009-03-09 01:21:12 -06006654
6655 return error;
6656}
6657
6658/**
Eric Moored5d135b2009-05-18 13:02:08 -06006659 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06006660 *
6661 * Returns 0 success, anything else error.
6662 */
6663static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06006664_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06006665{
6666 printk(KERN_INFO "mpt2sas version %s unloading\n",
6667 MPT2SAS_DRIVER_VERSION);
6668
6669 pci_unregister_driver(&scsih_driver);
6670
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306671 mpt2sas_ctl_exit();
6672
Eric Moore635374e2009-03-09 01:21:12 -06006673 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
6674 mpt2sas_base_release_callback_handler(tm_cb_idx);
6675 mpt2sas_base_release_callback_handler(base_cb_idx);
6676 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306677 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06006678 mpt2sas_base_release_callback_handler(config_cb_idx);
6679 mpt2sas_base_release_callback_handler(ctl_cb_idx);
6680
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306681 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
6682 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
6683
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306684 /* raid transport support */
6685 raid_class_release(mpt2sas_raid_template);
6686 sas_release_transport(mpt2sas_transport_template);
6687
Eric Moore635374e2009-03-09 01:21:12 -06006688}
6689
Eric Moored5d135b2009-05-18 13:02:08 -06006690module_init(_scsih_init);
6691module_exit(_scsih_exit);