blob: 59ea821c2a3c7e7e303284af22add95085b6c287 [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>
55
56#include "mpt2sas_base.h"
57
58MODULE_AUTHOR(MPT2SAS_AUTHOR);
59MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60MODULE_LICENSE("GPL");
61MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63#define RAID_CHANNEL 1
64
65/* forward proto's */
66static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67 struct _sas_node *sas_expander);
68static void _firmware_event_work(struct work_struct *work);
69
70/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060071LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060072
73/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060074static u8 scsi_io_cb_idx = -1;
75static u8 tm_cb_idx = -1;
76static u8 ctl_cb_idx = -1;
77static u8 base_cb_idx = -1;
78static u8 transport_cb_idx = -1;
79static u8 config_cb_idx = -1;
80static int mpt_ids;
81
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053082static u8 tm_tr_cb_idx = -1 ;
83static u8 tm_sas_control_cb_idx = -1;
84
Eric Moore635374e2009-03-09 01:21:12 -060085/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060086static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060087MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
88 "(default=0)");
89
90/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
91#define MPT2SAS_MAX_LUN (16895)
92static int max_lun = MPT2SAS_MAX_LUN;
93module_param(max_lun, int, 0);
94MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
95
96/**
97 * struct sense_info - common structure for obtaining sense keys
98 * @skey: sense key
99 * @asc: additional sense code
100 * @ascq: additional sense code qualifier
101 */
102struct sense_info {
103 u8 skey;
104 u8 asc;
105 u8 ascq;
106};
107
108
Eric Moore635374e2009-03-09 01:21:12 -0600109/**
110 * struct fw_event_work - firmware event struct
111 * @list: link list framework
112 * @work: work object (ioc->fault_reset_work_q)
113 * @ioc: per adapter object
114 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530115 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600116 * @host_reset_handling: handling events during host reset
117 * @ignore: flag meaning this event has been marked to ignore
118 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
119 * @event_data: reply event data payload follows
120 *
121 * This object stored on ioc->fw_event_list.
122 */
123struct fw_event_work {
124 struct list_head list;
Eric Moore6f92a7a2009-04-21 15:43:33 -0600125 struct work_struct work;
Eric Moore635374e2009-03-09 01:21:12 -0600126 struct MPT2SAS_ADAPTER *ioc;
127 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530128 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600129 u8 host_reset_handling;
130 u8 ignore;
131 u16 event;
132 void *event_data;
133};
134
135/**
136 * struct _scsi_io_transfer - scsi io transfer
137 * @handle: sas device handle (assigned by firmware)
138 * @is_raid: flag set for hidden raid components
139 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
140 * @data_length: data transfer length
141 * @data_dma: dma pointer to data
142 * @sense: sense data
143 * @lun: lun number
144 * @cdb_length: cdb length
145 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600146 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530147 * @VF_ID: virtual function id
148 * @VP_ID: virtual port id
149 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600150 * @sense_length: sense length
151 * @ioc_status: ioc status
152 * @scsi_state: scsi state
153 * @scsi_status: scsi staus
154 * @log_info: log information
155 * @transfer_length: data length transfer when there is a reply message
156 *
157 * Used for sending internal scsi commands to devices within this module.
158 * Refer to _scsi_send_scsi_io().
159 */
160struct _scsi_io_transfer {
161 u16 handle;
162 u8 is_raid;
163 enum dma_data_direction dir;
164 u32 data_length;
165 dma_addr_t data_dma;
166 u8 sense[SCSI_SENSE_BUFFERSIZE];
167 u32 lun;
168 u8 cdb_length;
169 u8 cdb[32];
170 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530171 u8 VF_ID;
172 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600173 u8 valid_reply;
174 /* the following bits are only valid when 'valid_reply = 1' */
175 u32 sense_length;
176 u16 ioc_status;
177 u8 scsi_state;
178 u8 scsi_status;
179 u32 log_info;
180 u32 transfer_length;
181};
182
183/*
184 * The pci device ids are defined in mpi/mpi2_cnfg.h.
185 */
186static struct pci_device_id scsih_pci_table[] = {
187 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
188 PCI_ANY_ID, PCI_ANY_ID },
189 /* Falcon ~ 2008*/
190 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
191 PCI_ANY_ID, PCI_ANY_ID },
192 /* Liberator ~ 2108 */
193 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
194 PCI_ANY_ID, PCI_ANY_ID },
195 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
196 PCI_ANY_ID, PCI_ANY_ID },
197 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
198 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530199 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600200 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
201 PCI_ANY_ID, PCI_ANY_ID },
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
203 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530204 /* Thunderbolt ~ 2208 */
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
206 PCI_ANY_ID, PCI_ANY_ID },
207 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
208 PCI_ANY_ID, PCI_ANY_ID },
209 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
210 PCI_ANY_ID, PCI_ANY_ID },
211 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
212 PCI_ANY_ID, PCI_ANY_ID },
213 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
214 PCI_ANY_ID, PCI_ANY_ID },
215 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
216 PCI_ANY_ID, PCI_ANY_ID },
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
220 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600221 {0} /* Terminating entry */
222};
223MODULE_DEVICE_TABLE(pci, scsih_pci_table);
224
225/**
Eric Moored5d135b2009-05-18 13:02:08 -0600226 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600227 *
228 * Note: The logging levels are defined in mpt2sas_debug.h.
229 */
230static int
Eric Moored5d135b2009-05-18 13:02:08 -0600231_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600232{
233 int ret = param_set_int(val, kp);
234 struct MPT2SAS_ADAPTER *ioc;
235
236 if (ret)
237 return ret;
238
239 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600240 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600241 ioc->logging_level = logging_level;
242 return 0;
243}
Eric Moored5d135b2009-05-18 13:02:08 -0600244module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600245 &logging_level, 0644);
246
247/**
248 * _scsih_srch_boot_sas_address - search based on sas_address
249 * @sas_address: sas address
250 * @boot_device: boot device object from bios page 2
251 *
252 * Returns 1 when there's a match, 0 means no match.
253 */
254static inline int
255_scsih_srch_boot_sas_address(u64 sas_address,
256 Mpi2BootDeviceSasWwid_t *boot_device)
257{
258 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
259}
260
261/**
262 * _scsih_srch_boot_device_name - search based on device name
263 * @device_name: device name specified in INDENTIFY fram
264 * @boot_device: boot device object from bios page 2
265 *
266 * Returns 1 when there's a match, 0 means no match.
267 */
268static inline int
269_scsih_srch_boot_device_name(u64 device_name,
270 Mpi2BootDeviceDeviceName_t *boot_device)
271{
272 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
273}
274
275/**
276 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
277 * @enclosure_logical_id: enclosure logical id
278 * @slot_number: slot number
279 * @boot_device: boot device object from bios page 2
280 *
281 * Returns 1 when there's a match, 0 means no match.
282 */
283static inline int
284_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
285 Mpi2BootDeviceEnclosureSlot_t *boot_device)
286{
287 return (enclosure_logical_id == le64_to_cpu(boot_device->
288 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
289 SlotNumber)) ? 1 : 0;
290}
291
292/**
293 * _scsih_is_boot_device - search for matching boot device.
294 * @sas_address: sas address
295 * @device_name: device name specified in INDENTIFY fram
296 * @enclosure_logical_id: enclosure logical id
297 * @slot_number: slot number
298 * @form: specifies boot device form
299 * @boot_device: boot device object from bios page 2
300 *
301 * Returns 1 when there's a match, 0 means no match.
302 */
303static int
304_scsih_is_boot_device(u64 sas_address, u64 device_name,
305 u64 enclosure_logical_id, u16 slot, u8 form,
306 Mpi2BiosPage2BootDevice_t *boot_device)
307{
308 int rc = 0;
309
310 switch (form) {
311 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
312 if (!sas_address)
313 break;
314 rc = _scsih_srch_boot_sas_address(
315 sas_address, &boot_device->SasWwid);
316 break;
317 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
318 if (!enclosure_logical_id)
319 break;
320 rc = _scsih_srch_boot_encl_slot(
321 enclosure_logical_id,
322 slot, &boot_device->EnclosureSlot);
323 break;
324 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
325 if (!device_name)
326 break;
327 rc = _scsih_srch_boot_device_name(
328 device_name, &boot_device->DeviceName);
329 break;
330 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
331 break;
332 }
333
334 return rc;
335}
336
337/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530338 * _scsih_get_sas_address - set the sas_address for given device handle
339 * @handle: device handle
340 * @sas_address: sas address
341 *
342 * Returns 0 success, non-zero when failure
343 */
344static int
345_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
346 u64 *sas_address)
347{
348 Mpi2SasDevicePage0_t sas_device_pg0;
349 Mpi2ConfigReply_t mpi_reply;
350 u32 ioc_status;
351
352 if (handle <= ioc->sas_hba.num_phys) {
353 *sas_address = ioc->sas_hba.sas_address;
354 return 0;
355 } else
356 *sas_address = 0;
357
358 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
359 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
360 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
361 ioc->name, __FILE__, __LINE__, __func__);
362 return -ENXIO;
363 }
364
365 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
366 MPI2_IOCSTATUS_MASK;
367 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
368 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
369 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
370 __FILE__, __LINE__, __func__);
371 return -EIO;
372 }
373
374 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
375 return 0;
376}
377
378/**
Eric Moore635374e2009-03-09 01:21:12 -0600379 * _scsih_determine_boot_device - determine boot device.
380 * @ioc: per adapter object
381 * @device: either sas_device or raid_device object
382 * @is_raid: [flag] 1 = raid object, 0 = sas object
383 *
384 * Determines whether this device should be first reported device to
385 * to scsi-ml or sas transport, this purpose is for persistant boot device.
386 * There are primary, alternate, and current entries in bios page 2. The order
387 * priority is primary, alternate, then current. This routine saves
388 * the corresponding device object and is_raid flag in the ioc object.
389 * The saved data to be used later in _scsih_probe_boot_devices().
390 */
391static void
392_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
393 void *device, u8 is_raid)
394{
395 struct _sas_device *sas_device;
396 struct _raid_device *raid_device;
397 u64 sas_address;
398 u64 device_name;
399 u64 enclosure_logical_id;
400 u16 slot;
401
402 /* only process this function when driver loads */
403 if (!ioc->wait_for_port_enable_to_complete)
404 return;
405
406 if (!is_raid) {
407 sas_device = device;
408 sas_address = sas_device->sas_address;
409 device_name = sas_device->device_name;
410 enclosure_logical_id = sas_device->enclosure_logical_id;
411 slot = sas_device->slot;
412 } else {
413 raid_device = device;
414 sas_address = raid_device->wwid;
415 device_name = 0;
416 enclosure_logical_id = 0;
417 slot = 0;
418 }
419
420 if (!ioc->req_boot_device.device) {
421 if (_scsih_is_boot_device(sas_address, device_name,
422 enclosure_logical_id, slot,
423 (ioc->bios_pg2.ReqBootDeviceForm &
424 MPI2_BIOSPAGE2_FORM_MASK),
425 &ioc->bios_pg2.RequestedBootDevice)) {
426 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
427 "%s: req_boot_device(0x%016llx)\n",
428 ioc->name, __func__,
429 (unsigned long long)sas_address));
430 ioc->req_boot_device.device = device;
431 ioc->req_boot_device.is_raid = is_raid;
432 }
433 }
434
435 if (!ioc->req_alt_boot_device.device) {
436 if (_scsih_is_boot_device(sas_address, device_name,
437 enclosure_logical_id, slot,
438 (ioc->bios_pg2.ReqAltBootDeviceForm &
439 MPI2_BIOSPAGE2_FORM_MASK),
440 &ioc->bios_pg2.RequestedAltBootDevice)) {
441 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
442 "%s: req_alt_boot_device(0x%016llx)\n",
443 ioc->name, __func__,
444 (unsigned long long)sas_address));
445 ioc->req_alt_boot_device.device = device;
446 ioc->req_alt_boot_device.is_raid = is_raid;
447 }
448 }
449
450 if (!ioc->current_boot_device.device) {
451 if (_scsih_is_boot_device(sas_address, device_name,
452 enclosure_logical_id, slot,
453 (ioc->bios_pg2.CurrentBootDeviceForm &
454 MPI2_BIOSPAGE2_FORM_MASK),
455 &ioc->bios_pg2.CurrentBootDevice)) {
456 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
457 "%s: current_boot_device(0x%016llx)\n",
458 ioc->name, __func__,
459 (unsigned long long)sas_address));
460 ioc->current_boot_device.device = device;
461 ioc->current_boot_device.is_raid = is_raid;
462 }
463 }
464}
465
466/**
467 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
468 * @ioc: per adapter object
469 * @sas_address: sas address
470 * Context: Calling function should acquire ioc->sas_device_lock
471 *
472 * This searches for sas_device based on sas_address, then return sas_device
473 * object.
474 */
475struct _sas_device *
476mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
477 u64 sas_address)
478{
479 struct _sas_device *sas_device, *r;
480
481 r = NULL;
482 /* check the sas_device_init_list */
483 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
484 list) {
485 if (sas_device->sas_address != sas_address)
486 continue;
487 r = sas_device;
488 goto out;
489 }
490
491 /* then check the sas_device_list */
492 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
493 if (sas_device->sas_address != sas_address)
494 continue;
495 r = sas_device;
496 goto out;
497 }
498 out:
499 return r;
500}
501
502/**
503 * _scsih_sas_device_find_by_handle - sas device search
504 * @ioc: per adapter object
505 * @handle: sas device handle (assigned by firmware)
506 * Context: Calling function should acquire ioc->sas_device_lock
507 *
508 * This searches for sas_device based on sas_address, then return sas_device
509 * object.
510 */
511static struct _sas_device *
512_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
513{
514 struct _sas_device *sas_device, *r;
515
516 r = NULL;
517 if (ioc->wait_for_port_enable_to_complete) {
518 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
519 list) {
520 if (sas_device->handle != handle)
521 continue;
522 r = sas_device;
523 goto out;
524 }
525 } else {
526 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
527 if (sas_device->handle != handle)
528 continue;
529 r = sas_device;
530 goto out;
531 }
532 }
533
534 out:
535 return r;
536}
537
538/**
539 * _scsih_sas_device_remove - remove sas_device from list.
540 * @ioc: per adapter object
541 * @sas_device: the sas_device object
542 * Context: This function will acquire ioc->sas_device_lock.
543 *
544 * Removing object and freeing associated memory from the ioc->sas_device_list.
545 */
546static void
547_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
548 struct _sas_device *sas_device)
549{
550 unsigned long flags;
551
552 spin_lock_irqsave(&ioc->sas_device_lock, flags);
553 list_del(&sas_device->list);
554 memset(sas_device, 0, sizeof(struct _sas_device));
555 kfree(sas_device);
556 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
557}
558
559/**
560 * _scsih_sas_device_add - insert sas_device to the list.
561 * @ioc: per adapter object
562 * @sas_device: the sas_device object
563 * Context: This function will acquire ioc->sas_device_lock.
564 *
565 * Adding new object to the ioc->sas_device_list.
566 */
567static void
568_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
569 struct _sas_device *sas_device)
570{
571 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600572
573 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
574 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
575 sas_device->handle, (unsigned long long)sas_device->sas_address));
576
577 spin_lock_irqsave(&ioc->sas_device_lock, flags);
578 list_add_tail(&sas_device->list, &ioc->sas_device_list);
579 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
580
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530581 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
582 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600583 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600584}
585
586/**
587 * _scsih_sas_device_init_add - insert sas_device to the list.
588 * @ioc: per adapter object
589 * @sas_device: the sas_device object
590 * Context: This function will acquire ioc->sas_device_lock.
591 *
592 * Adding new object at driver load time to the ioc->sas_device_init_list.
593 */
594static void
595_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
596 struct _sas_device *sas_device)
597{
598 unsigned long flags;
599
600 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
601 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
602 sas_device->handle, (unsigned long long)sas_device->sas_address));
603
604 spin_lock_irqsave(&ioc->sas_device_lock, flags);
605 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
606 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
607 _scsih_determine_boot_device(ioc, sas_device, 0);
608}
609
610/**
Eric Moore635374e2009-03-09 01:21:12 -0600611 * _scsih_raid_device_find_by_id - raid device search
612 * @ioc: per adapter object
613 * @id: sas device target id
614 * @channel: sas device channel
615 * Context: Calling function should acquire ioc->raid_device_lock
616 *
617 * This searches for raid_device based on target id, then return raid_device
618 * object.
619 */
620static struct _raid_device *
621_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
622{
623 struct _raid_device *raid_device, *r;
624
625 r = NULL;
626 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
627 if (raid_device->id == id && raid_device->channel == channel) {
628 r = raid_device;
629 goto out;
630 }
631 }
632
633 out:
634 return r;
635}
636
637/**
638 * _scsih_raid_device_find_by_handle - raid device search
639 * @ioc: per adapter object
640 * @handle: sas device handle (assigned by firmware)
641 * Context: Calling function should acquire ioc->raid_device_lock
642 *
643 * This searches for raid_device based on handle, then return raid_device
644 * object.
645 */
646static struct _raid_device *
647_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
648{
649 struct _raid_device *raid_device, *r;
650
651 r = NULL;
652 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
653 if (raid_device->handle != handle)
654 continue;
655 r = raid_device;
656 goto out;
657 }
658
659 out:
660 return r;
661}
662
663/**
664 * _scsih_raid_device_find_by_wwid - raid device search
665 * @ioc: per adapter object
666 * @handle: sas device handle (assigned by firmware)
667 * Context: Calling function should acquire ioc->raid_device_lock
668 *
669 * This searches for raid_device based on wwid, then return raid_device
670 * object.
671 */
672static struct _raid_device *
673_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
674{
675 struct _raid_device *raid_device, *r;
676
677 r = NULL;
678 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
679 if (raid_device->wwid != wwid)
680 continue;
681 r = raid_device;
682 goto out;
683 }
684
685 out:
686 return r;
687}
688
689/**
690 * _scsih_raid_device_add - add raid_device object
691 * @ioc: per adapter object
692 * @raid_device: raid_device object
693 *
694 * This is added to the raid_device_list link list.
695 */
696static void
697_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
698 struct _raid_device *raid_device)
699{
700 unsigned long flags;
701
702 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
703 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
704 raid_device->handle, (unsigned long long)raid_device->wwid));
705
706 spin_lock_irqsave(&ioc->raid_device_lock, flags);
707 list_add_tail(&raid_device->list, &ioc->raid_device_list);
708 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
709}
710
711/**
712 * _scsih_raid_device_remove - delete raid_device object
713 * @ioc: per adapter object
714 * @raid_device: raid_device object
715 *
716 * This is removed from the raid_device_list link list.
717 */
718static void
719_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
720 struct _raid_device *raid_device)
721{
722 unsigned long flags;
723
724 spin_lock_irqsave(&ioc->raid_device_lock, flags);
725 list_del(&raid_device->list);
726 memset(raid_device, 0, sizeof(struct _raid_device));
727 kfree(raid_device);
728 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
729}
730
731/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530732 * mpt2sas_scsih_expander_find_by_handle - expander device search
733 * @ioc: per adapter object
734 * @handle: expander handle (assigned by firmware)
735 * Context: Calling function should acquire ioc->sas_device_lock
736 *
737 * This searches for expander device based on handle, then returns the
738 * sas_node object.
739 */
740struct _sas_node *
741mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
742{
743 struct _sas_node *sas_expander, *r;
744
745 r = NULL;
746 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
747 if (sas_expander->handle != handle)
748 continue;
749 r = sas_expander;
750 goto out;
751 }
752 out:
753 return r;
754}
755
756/**
Eric Moore635374e2009-03-09 01:21:12 -0600757 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
758 * @ioc: per adapter object
759 * @sas_address: sas address
760 * Context: Calling function should acquire ioc->sas_node_lock.
761 *
762 * This searches for expander device based on sas_address, then returns the
763 * sas_node object.
764 */
765struct _sas_node *
766mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
767 u64 sas_address)
768{
769 struct _sas_node *sas_expander, *r;
770
771 r = NULL;
772 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
773 if (sas_expander->sas_address != sas_address)
774 continue;
775 r = sas_expander;
776 goto out;
777 }
778 out:
779 return r;
780}
781
782/**
783 * _scsih_expander_node_add - insert expander device to the list.
784 * @ioc: per adapter object
785 * @sas_expander: the sas_device object
786 * Context: This function will acquire ioc->sas_node_lock.
787 *
788 * Adding new object to the ioc->sas_expander_list.
789 *
790 * Return nothing.
791 */
792static void
793_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
794 struct _sas_node *sas_expander)
795{
796 unsigned long flags;
797
798 spin_lock_irqsave(&ioc->sas_node_lock, flags);
799 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
800 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
801}
802
803/**
804 * _scsih_is_end_device - determines if device is an end device
805 * @device_info: bitfield providing information about the device.
806 * Context: none
807 *
808 * Returns 1 if end device.
809 */
810static int
811_scsih_is_end_device(u32 device_info)
812{
813 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
814 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
815 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
816 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
817 return 1;
818 else
819 return 0;
820}
821
822/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530823 * mptscsih_get_scsi_lookup - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600824 * @ioc: per adapter object
825 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600826 *
827 * Returns the smid stored scmd pointer.
828 */
829static struct scsi_cmnd *
830_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
831{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530832 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600833}
834
835/**
836 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
837 * @ioc: per adapter object
838 * @smid: system request message index
839 * @scmd: pointer to scsi command object
840 * Context: This function will acquire ioc->scsi_lookup_lock.
841 *
842 * This will search for a scmd pointer in the scsi_lookup array,
843 * returning the revelent smid. A returned value of zero means invalid.
844 */
845static u16
846_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
847 *scmd)
848{
849 u16 smid;
850 unsigned long flags;
851 int i;
852
853 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
854 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530855 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600856 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530857 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600858 goto out;
859 }
860 }
861 out:
862 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
863 return smid;
864}
865
866/**
867 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
868 * @ioc: per adapter object
869 * @id: target id
870 * @channel: channel
871 * Context: This function will acquire ioc->scsi_lookup_lock.
872 *
873 * This will search for a matching channel:id in the scsi_lookup array,
874 * returning 1 if found.
875 */
876static u8
877_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
878 int channel)
879{
880 u8 found;
881 unsigned long flags;
882 int i;
883
884 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
885 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530886 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600887 if (ioc->scsi_lookup[i].scmd &&
888 (ioc->scsi_lookup[i].scmd->device->id == id &&
889 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
890 found = 1;
891 goto out;
892 }
893 }
894 out:
895 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
896 return found;
897}
898
899/**
Eric Moore993e0da2009-05-18 13:00:45 -0600900 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
901 * @ioc: per adapter object
902 * @id: target id
903 * @lun: lun number
904 * @channel: channel
905 * Context: This function will acquire ioc->scsi_lookup_lock.
906 *
907 * This will search for a matching channel:id:lun in the scsi_lookup array,
908 * returning 1 if found.
909 */
910static u8
911_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
912 unsigned int lun, int channel)
913{
914 u8 found;
915 unsigned long flags;
916 int i;
917
918 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
919 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530920 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600921 if (ioc->scsi_lookup[i].scmd &&
922 (ioc->scsi_lookup[i].scmd->device->id == id &&
923 ioc->scsi_lookup[i].scmd->device->channel == channel &&
924 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
925 found = 1;
926 goto out;
927 }
928 }
929 out:
930 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
931 return found;
932}
933
934/**
Eric Moore635374e2009-03-09 01:21:12 -0600935 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
936 * @ioc: per adapter object
937 * @smid: system request message index
938 *
939 * Returns phys pointer to chain buffer.
940 */
941static dma_addr_t
942_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
943{
944 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
945 ioc->chains_needed_per_io));
946}
947
948/**
949 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
950 * @ioc: per adapter object
951 * @smid: system request message index
952 *
953 * Returns virt pointer to chain buffer.
954 */
955static void *
956_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
957{
958 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
959 ioc->chains_needed_per_io)));
960}
961
962/**
963 * _scsih_build_scatter_gather - main sg creation routine
964 * @ioc: per adapter object
965 * @scmd: scsi command
966 * @smid: system request message index
967 * Context: none.
968 *
969 * The main routine that builds scatter gather table from a given
970 * scsi request sent via the .queuecommand main handler.
971 *
972 * Returns 0 success, anything else error
973 */
974static int
975_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
976 struct scsi_cmnd *scmd, u16 smid)
977{
978 Mpi2SCSIIORequest_t *mpi_request;
979 dma_addr_t chain_dma;
980 struct scatterlist *sg_scmd;
981 void *sg_local, *chain;
982 u32 chain_offset;
983 u32 chain_length;
984 u32 chain_flags;
985 u32 sges_left;
986 u32 sges_in_segment;
987 u32 sgl_flags;
988 u32 sgl_flags_last_element;
989 u32 sgl_flags_end_buffer;
990
991 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
992
993 /* init scatter gather flags */
994 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
995 if (scmd->sc_data_direction == DMA_TO_DEVICE)
996 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
997 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
998 << MPI2_SGE_FLAGS_SHIFT;
999 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1000 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1001 << MPI2_SGE_FLAGS_SHIFT;
1002 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1003
1004 sg_scmd = scsi_sglist(scmd);
1005 sges_left = scsi_dma_map(scmd);
1006 if (!sges_left) {
1007 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1008 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1009 return -ENOMEM;
1010 }
1011
1012 sg_local = &mpi_request->SGL;
1013 sges_in_segment = ioc->max_sges_in_main_message;
1014 if (sges_left <= sges_in_segment)
1015 goto fill_in_last_segment;
1016
1017 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1018 (sges_in_segment * ioc->sge_size))/4;
1019
1020 /* fill in main message segment when there is a chain following */
1021 while (sges_in_segment) {
1022 if (sges_in_segment == 1)
1023 ioc->base_add_sg_single(sg_local,
1024 sgl_flags_last_element | sg_dma_len(sg_scmd),
1025 sg_dma_address(sg_scmd));
1026 else
1027 ioc->base_add_sg_single(sg_local, sgl_flags |
1028 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1029 sg_scmd = sg_next(sg_scmd);
1030 sg_local += ioc->sge_size;
1031 sges_left--;
1032 sges_in_segment--;
1033 }
1034
1035 /* initializing the chain flags and pointers */
1036 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1037 chain = _scsih_get_chain_buffer(ioc, smid);
1038 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1039 do {
1040 sges_in_segment = (sges_left <=
1041 ioc->max_sges_in_chain_message) ? sges_left :
1042 ioc->max_sges_in_chain_message;
1043 chain_offset = (sges_left == sges_in_segment) ?
1044 0 : (sges_in_segment * ioc->sge_size)/4;
1045 chain_length = sges_in_segment * ioc->sge_size;
1046 if (chain_offset) {
1047 chain_offset = chain_offset <<
1048 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1049 chain_length += ioc->sge_size;
1050 }
1051 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1052 chain_length, chain_dma);
1053 sg_local = chain;
1054 if (!chain_offset)
1055 goto fill_in_last_segment;
1056
1057 /* fill in chain segments */
1058 while (sges_in_segment) {
1059 if (sges_in_segment == 1)
1060 ioc->base_add_sg_single(sg_local,
1061 sgl_flags_last_element |
1062 sg_dma_len(sg_scmd),
1063 sg_dma_address(sg_scmd));
1064 else
1065 ioc->base_add_sg_single(sg_local, sgl_flags |
1066 sg_dma_len(sg_scmd),
1067 sg_dma_address(sg_scmd));
1068 sg_scmd = sg_next(sg_scmd);
1069 sg_local += ioc->sge_size;
1070 sges_left--;
1071 sges_in_segment--;
1072 }
1073
1074 chain_dma += ioc->request_sz;
1075 chain += ioc->request_sz;
1076 } while (1);
1077
1078
1079 fill_in_last_segment:
1080
1081 /* fill the last segment */
1082 while (sges_left) {
1083 if (sges_left == 1)
1084 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1085 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1086 else
1087 ioc->base_add_sg_single(sg_local, sgl_flags |
1088 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1089 sg_scmd = sg_next(sg_scmd);
1090 sg_local += ioc->sge_size;
1091 sges_left--;
1092 }
1093
1094 return 0;
1095}
1096
1097/**
Eric Moored5d135b2009-05-18 13:02:08 -06001098 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001099 * @sdev: scsi device struct
1100 * @qdepth: requested queue depth
1101 *
1102 * Returns queue depth.
1103 */
1104static int
Eric Moored5d135b2009-05-18 13:02:08 -06001105_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001106{
1107 struct Scsi_Host *shost = sdev->host;
1108 int max_depth;
1109 int tag_type;
1110
1111 max_depth = shost->can_queue;
1112 if (!sdev->tagged_supported)
1113 max_depth = 1;
1114 if (qdepth > max_depth)
1115 qdepth = max_depth;
1116 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1117 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1118
1119 if (sdev->inquiry_len > 7)
1120 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1121 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1122 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1123 sdev->ordered_tags, sdev->scsi_level,
1124 (sdev->inquiry[7] & 2) >> 1);
1125
1126 return sdev->queue_depth;
1127}
1128
1129/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301130 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001131 * @sdev: scsi device struct
1132 * @tag_type: requested tag type
1133 *
1134 * Returns queue tag type.
1135 */
1136static int
Eric Moored5d135b2009-05-18 13:02:08 -06001137_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001138{
1139 if (sdev->tagged_supported) {
1140 scsi_set_tag_type(sdev, tag_type);
1141 if (tag_type)
1142 scsi_activate_tcq(sdev, sdev->queue_depth);
1143 else
1144 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1145 } else
1146 tag_type = 0;
1147
1148 return tag_type;
1149}
1150
1151/**
Eric Moored5d135b2009-05-18 13:02:08 -06001152 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001153 * @starget: scsi target struct
1154 *
1155 * Returns 0 if ok. Any other return is assumed to be an error and
1156 * the device is ignored.
1157 */
1158static int
Eric Moored5d135b2009-05-18 13:02:08 -06001159_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001160{
1161 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1162 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1163 struct MPT2SAS_TARGET *sas_target_priv_data;
1164 struct _sas_device *sas_device;
1165 struct _raid_device *raid_device;
1166 unsigned long flags;
1167 struct sas_rphy *rphy;
1168
1169 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1170 if (!sas_target_priv_data)
1171 return -ENOMEM;
1172
1173 starget->hostdata = sas_target_priv_data;
1174 sas_target_priv_data->starget = starget;
1175 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1176
1177 /* RAID volumes */
1178 if (starget->channel == RAID_CHANNEL) {
1179 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1180 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1181 starget->channel);
1182 if (raid_device) {
1183 sas_target_priv_data->handle = raid_device->handle;
1184 sas_target_priv_data->sas_address = raid_device->wwid;
1185 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1186 raid_device->starget = starget;
1187 }
1188 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1189 return 0;
1190 }
1191
1192 /* sas/sata devices */
1193 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1194 rphy = dev_to_rphy(starget->dev.parent);
1195 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1196 rphy->identify.sas_address);
1197
1198 if (sas_device) {
1199 sas_target_priv_data->handle = sas_device->handle;
1200 sas_target_priv_data->sas_address = sas_device->sas_address;
1201 sas_device->starget = starget;
1202 sas_device->id = starget->id;
1203 sas_device->channel = starget->channel;
1204 if (sas_device->hidden_raid_component)
1205 sas_target_priv_data->flags |=
1206 MPT_TARGET_FLAGS_RAID_COMPONENT;
1207 }
1208 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1209
1210 return 0;
1211}
1212
1213/**
Eric Moored5d135b2009-05-18 13:02:08 -06001214 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001215 * @starget: scsi target struct
1216 *
1217 * Returns nothing.
1218 */
1219static void
Eric Moored5d135b2009-05-18 13:02:08 -06001220_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001221{
1222 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1223 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1224 struct MPT2SAS_TARGET *sas_target_priv_data;
1225 struct _sas_device *sas_device;
1226 struct _raid_device *raid_device;
1227 unsigned long flags;
1228 struct sas_rphy *rphy;
1229
1230 sas_target_priv_data = starget->hostdata;
1231 if (!sas_target_priv_data)
1232 return;
1233
1234 if (starget->channel == RAID_CHANNEL) {
1235 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1236 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1237 starget->channel);
1238 if (raid_device) {
1239 raid_device->starget = NULL;
1240 raid_device->sdev = NULL;
1241 }
1242 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1243 goto out;
1244 }
1245
1246 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1247 rphy = dev_to_rphy(starget->dev.parent);
1248 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1249 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001250 if (sas_device && (sas_device->starget == starget) &&
1251 (sas_device->id == starget->id) &&
1252 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001253 sas_device->starget = NULL;
1254
1255 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1256
1257 out:
1258 kfree(sas_target_priv_data);
1259 starget->hostdata = NULL;
1260}
1261
1262/**
Eric Moored5d135b2009-05-18 13:02:08 -06001263 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001264 * @sdev: scsi device struct
1265 *
1266 * Returns 0 if ok. Any other return is assumed to be an error and
1267 * the device is ignored.
1268 */
1269static int
Eric Moored5d135b2009-05-18 13:02:08 -06001270_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001271{
1272 struct Scsi_Host *shost;
1273 struct MPT2SAS_ADAPTER *ioc;
1274 struct MPT2SAS_TARGET *sas_target_priv_data;
1275 struct MPT2SAS_DEVICE *sas_device_priv_data;
1276 struct scsi_target *starget;
1277 struct _raid_device *raid_device;
1278 struct _sas_device *sas_device;
1279 unsigned long flags;
1280
1281 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1282 if (!sas_device_priv_data)
1283 return -ENOMEM;
1284
1285 sas_device_priv_data->lun = sdev->lun;
1286 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1287
1288 starget = scsi_target(sdev);
1289 sas_target_priv_data = starget->hostdata;
1290 sas_target_priv_data->num_luns++;
1291 sas_device_priv_data->sas_target = sas_target_priv_data;
1292 sdev->hostdata = sas_device_priv_data;
1293 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1294 sdev->no_uld_attach = 1;
1295
1296 shost = dev_to_shost(&starget->dev);
1297 ioc = shost_priv(shost);
1298 if (starget->channel == RAID_CHANNEL) {
1299 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1300 raid_device = _scsih_raid_device_find_by_id(ioc,
1301 starget->id, starget->channel);
1302 if (raid_device)
1303 raid_device->sdev = sdev; /* raid is single lun */
1304 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1305 } else {
1306 /* set TLR bit for SSP devices */
1307 if (!(ioc->facts.IOCCapabilities &
1308 MPI2_IOCFACTS_CAPABILITY_TLR))
1309 goto out;
1310 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1311 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1312 sas_device_priv_data->sas_target->sas_address);
1313 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1314 if (sas_device && sas_device->device_info &
1315 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1316 sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1317 }
1318
1319 out:
1320 return 0;
1321}
1322
1323/**
Eric Moored5d135b2009-05-18 13:02:08 -06001324 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001325 * @sdev: scsi device struct
1326 *
1327 * Returns nothing.
1328 */
1329static void
Eric Moored5d135b2009-05-18 13:02:08 -06001330_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001331{
1332 struct MPT2SAS_TARGET *sas_target_priv_data;
1333 struct scsi_target *starget;
1334
1335 if (!sdev->hostdata)
1336 return;
1337
1338 starget = scsi_target(sdev);
1339 sas_target_priv_data = starget->hostdata;
1340 sas_target_priv_data->num_luns--;
1341 kfree(sdev->hostdata);
1342 sdev->hostdata = NULL;
1343}
1344
1345/**
Eric Moored5d135b2009-05-18 13:02:08 -06001346 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001347 * @ioc: per adapter object
1348 * @sas_device: the sas_device object
1349 * @sdev: scsi device struct
1350 */
1351static void
Eric Moored5d135b2009-05-18 13:02:08 -06001352_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001353 struct _sas_device *sas_device, struct scsi_device *sdev)
1354{
1355 Mpi2ConfigReply_t mpi_reply;
1356 Mpi2SasDevicePage0_t sas_device_pg0;
1357 u32 ioc_status;
1358 u16 flags;
1359 u32 device_info;
1360
1361 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1362 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1363 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1364 ioc->name, __FILE__, __LINE__, __func__);
1365 return;
1366 }
1367
1368 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1369 MPI2_IOCSTATUS_MASK;
1370 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1371 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1372 ioc->name, __FILE__, __LINE__, __func__);
1373 return;
1374 }
1375
1376 flags = le16_to_cpu(sas_device_pg0.Flags);
1377 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1378
1379 sdev_printk(KERN_INFO, sdev,
1380 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1381 "sw_preserve(%s)\n",
1382 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1383 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1384 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1385 "n",
1386 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1387 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1388 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1389}
1390
1391/**
1392 * _scsih_get_volume_capabilities - volume capabilities
1393 * @ioc: per adapter object
1394 * @sas_device: the raid_device object
1395 */
1396static void
1397_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1398 struct _raid_device *raid_device)
1399{
1400 Mpi2RaidVolPage0_t *vol_pg0;
1401 Mpi2RaidPhysDiskPage0_t pd_pg0;
1402 Mpi2SasDevicePage0_t sas_device_pg0;
1403 Mpi2ConfigReply_t mpi_reply;
1404 u16 sz;
1405 u8 num_pds;
1406
1407 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1408 &num_pds)) || !num_pds) {
1409 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1410 ioc->name, __FILE__, __LINE__, __func__);
1411 return;
1412 }
1413
1414 raid_device->num_pds = num_pds;
1415 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1416 sizeof(Mpi2RaidVol0PhysDisk_t));
1417 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1418 if (!vol_pg0) {
1419 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1420 ioc->name, __FILE__, __LINE__, __func__);
1421 return;
1422 }
1423
1424 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1425 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1426 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1427 ioc->name, __FILE__, __LINE__, __func__);
1428 kfree(vol_pg0);
1429 return;
1430 }
1431
1432 raid_device->volume_type = vol_pg0->VolumeType;
1433
1434 /* figure out what the underlying devices are by
1435 * obtaining the device_info bits for the 1st device
1436 */
1437 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1438 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1439 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1440 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1441 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1442 le16_to_cpu(pd_pg0.DevHandle)))) {
1443 raid_device->device_info =
1444 le32_to_cpu(sas_device_pg0.DeviceInfo);
1445 }
1446 }
1447
1448 kfree(vol_pg0);
1449}
1450
1451/**
Eric Moored5d135b2009-05-18 13:02:08 -06001452 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001453 * @sdev: scsi device struct
1454 *
1455 * Returns 0 if ok. Any other return is assumed to be an error and
1456 * the device is ignored.
1457 */
1458static int
Eric Moored5d135b2009-05-18 13:02:08 -06001459_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001460{
1461 struct Scsi_Host *shost = sdev->host;
1462 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1463 struct MPT2SAS_DEVICE *sas_device_priv_data;
1464 struct MPT2SAS_TARGET *sas_target_priv_data;
1465 struct _sas_device *sas_device;
1466 struct _raid_device *raid_device;
1467 unsigned long flags;
1468 int qdepth;
1469 u8 ssp_target = 0;
1470 char *ds = "";
1471 char *r_level = "";
1472
1473 qdepth = 1;
1474 sas_device_priv_data = sdev->hostdata;
1475 sas_device_priv_data->configured_lun = 1;
1476 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1477 sas_target_priv_data = sas_device_priv_data->sas_target;
1478
1479 /* raid volume handling */
1480 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1481
1482 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1483 raid_device = _scsih_raid_device_find_by_handle(ioc,
1484 sas_target_priv_data->handle);
1485 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1486 if (!raid_device) {
1487 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1488 ioc->name, __FILE__, __LINE__, __func__);
1489 return 0;
1490 }
1491
1492 _scsih_get_volume_capabilities(ioc, raid_device);
1493
1494 /* RAID Queue Depth Support
1495 * IS volume = underlying qdepth of drive type, either
1496 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1497 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1498 */
1499 if (raid_device->device_info &
1500 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1501 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1502 ds = "SSP";
1503 } else {
1504 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1505 if (raid_device->device_info &
1506 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1507 ds = "SATA";
1508 else
1509 ds = "STP";
1510 }
1511
1512 switch (raid_device->volume_type) {
1513 case MPI2_RAID_VOL_TYPE_RAID0:
1514 r_level = "RAID0";
1515 break;
1516 case MPI2_RAID_VOL_TYPE_RAID1E:
1517 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301518 if (ioc->manu_pg10.OEMIdentifier &&
1519 (ioc->manu_pg10.GenericFlags0 &
1520 MFG10_GF0_R10_DISPLAY) &&
1521 !(raid_device->num_pds % 2))
1522 r_level = "RAID10";
1523 else
1524 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001525 break;
1526 case MPI2_RAID_VOL_TYPE_RAID1:
1527 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1528 r_level = "RAID1";
1529 break;
1530 case MPI2_RAID_VOL_TYPE_RAID10:
1531 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1532 r_level = "RAID10";
1533 break;
1534 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1535 default:
1536 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1537 r_level = "RAIDX";
1538 break;
1539 }
1540
1541 sdev_printk(KERN_INFO, sdev, "%s: "
1542 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1543 r_level, raid_device->handle,
1544 (unsigned long long)raid_device->wwid,
1545 raid_device->num_pds, ds);
Eric Moored5d135b2009-05-18 13:02:08 -06001546 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001547 return 0;
1548 }
1549
1550 /* non-raid handling */
1551 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1552 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1553 sas_device_priv_data->sas_target->sas_address);
1554 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1555 if (sas_device) {
1556 if (sas_target_priv_data->flags &
1557 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1558 mpt2sas_config_get_volume_handle(ioc,
1559 sas_device->handle, &sas_device->volume_handle);
1560 mpt2sas_config_get_volume_wwid(ioc,
1561 sas_device->volume_handle,
1562 &sas_device->volume_wwid);
1563 }
1564 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1565 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1566 ssp_target = 1;
1567 ds = "SSP";
1568 } else {
1569 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1570 if (sas_device->device_info &
1571 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1572 ds = "STP";
1573 else if (sas_device->device_info &
1574 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1575 ds = "SATA";
1576 }
1577
1578 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1579 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1580 ds, sas_device->handle,
1581 (unsigned long long)sas_device->sas_address,
1582 (unsigned long long)sas_device->device_name);
1583 sdev_printk(KERN_INFO, sdev, "%s: "
1584 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1585 (unsigned long long) sas_device->enclosure_logical_id,
1586 sas_device->slot);
1587
1588 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001589 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001590 }
1591
Eric Moored5d135b2009-05-18 13:02:08 -06001592 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001593
1594 if (ssp_target)
1595 sas_read_port_mode_page(sdev);
1596 return 0;
1597}
1598
1599/**
Eric Moored5d135b2009-05-18 13:02:08 -06001600 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001601 * @sdev: scsi device struct
1602 * @bdev: pointer to block device context
1603 * @capacity: device size (in 512 byte sectors)
1604 * @params: three element array to place output:
1605 * params[0] number of heads (max 255)
1606 * params[1] number of sectors (max 63)
1607 * params[2] number of cylinders
1608 *
1609 * Return nothing.
1610 */
1611static int
Eric Moored5d135b2009-05-18 13:02:08 -06001612_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001613 sector_t capacity, int params[])
1614{
1615 int heads;
1616 int sectors;
1617 sector_t cylinders;
1618 ulong dummy;
1619
1620 heads = 64;
1621 sectors = 32;
1622
1623 dummy = heads * sectors;
1624 cylinders = capacity;
1625 sector_div(cylinders, dummy);
1626
1627 /*
1628 * Handle extended translation size for logical drives
1629 * > 1Gb
1630 */
1631 if ((ulong)capacity >= 0x200000) {
1632 heads = 255;
1633 sectors = 63;
1634 dummy = heads * sectors;
1635 cylinders = capacity;
1636 sector_div(cylinders, dummy);
1637 }
1638
1639 /* return result */
1640 params[0] = heads;
1641 params[1] = sectors;
1642 params[2] = cylinders;
1643
1644 return 0;
1645}
1646
1647/**
1648 * _scsih_response_code - translation of device response code
1649 * @ioc: per adapter object
1650 * @response_code: response code returned by the device
1651 *
1652 * Return nothing.
1653 */
1654static void
1655_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1656{
1657 char *desc;
1658
1659 switch (response_code) {
1660 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1661 desc = "task management request completed";
1662 break;
1663 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1664 desc = "invalid frame";
1665 break;
1666 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1667 desc = "task management request not supported";
1668 break;
1669 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1670 desc = "task management request failed";
1671 break;
1672 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1673 desc = "task management request succeeded";
1674 break;
1675 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1676 desc = "invalid lun";
1677 break;
1678 case 0xA:
1679 desc = "overlapped tag attempted";
1680 break;
1681 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1682 desc = "task queued, however not sent to target";
1683 break;
1684 default:
1685 desc = "unknown";
1686 break;
1687 }
1688 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1689 ioc->name, response_code, desc);
1690}
1691
1692/**
Eric Moored5d135b2009-05-18 13:02:08 -06001693 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001694 * @ioc: per adapter object
1695 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301696 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001697 * @reply: reply message frame(lower 32bit addr)
1698 * Context: none.
1699 *
1700 * The callback handler when using scsih_issue_tm.
1701 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301702 * Return 1 meaning mf should be freed from _base_interrupt
1703 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06001704 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301705static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301706_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001707{
1708 MPI2DefaultReply_t *mpi_reply;
1709
1710 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301711 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001712 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301713 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001714 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1715 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1716 if (mpi_reply) {
1717 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1718 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1719 }
1720 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1721 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301722 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001723}
1724
1725/**
1726 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1727 * @ioc: per adapter object
1728 * @handle: device handle
1729 *
1730 * During taskmangement request, we need to freeze the device queue.
1731 */
1732void
1733mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1734{
1735 struct MPT2SAS_DEVICE *sas_device_priv_data;
1736 struct scsi_device *sdev;
1737 u8 skip = 0;
1738
1739 shost_for_each_device(sdev, ioc->shost) {
1740 if (skip)
1741 continue;
1742 sas_device_priv_data = sdev->hostdata;
1743 if (!sas_device_priv_data)
1744 continue;
1745 if (sas_device_priv_data->sas_target->handle == handle) {
1746 sas_device_priv_data->sas_target->tm_busy = 1;
1747 skip = 1;
1748 ioc->ignore_loginfos = 1;
1749 }
1750 }
1751}
1752
1753/**
1754 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1755 * @ioc: per adapter object
1756 * @handle: device handle
1757 *
1758 * During taskmangement request, we need to freeze the device queue.
1759 */
1760void
1761mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1762{
1763 struct MPT2SAS_DEVICE *sas_device_priv_data;
1764 struct scsi_device *sdev;
1765 u8 skip = 0;
1766
1767 shost_for_each_device(sdev, ioc->shost) {
1768 if (skip)
1769 continue;
1770 sas_device_priv_data = sdev->hostdata;
1771 if (!sas_device_priv_data)
1772 continue;
1773 if (sas_device_priv_data->sas_target->handle == handle) {
1774 sas_device_priv_data->sas_target->tm_busy = 0;
1775 skip = 1;
1776 ioc->ignore_loginfos = 0;
1777 }
1778 }
1779}
1780
1781/**
1782 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1783 * @ioc: per adapter struct
1784 * @device_handle: device handle
1785 * @lun: lun number
1786 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1787 * @smid_task: smid assigned to the task
1788 * @timeout: timeout in seconds
1789 * Context: The calling function needs to acquire the tm_cmds.mutex
1790 *
1791 * A generic API for sending task management requests to firmware.
1792 *
1793 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1794 * this API.
1795 *
1796 * The callback index is set inside `ioc->tm_cb_idx`.
1797 *
1798 * Return nothing.
1799 */
1800void
1801mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1802 u8 type, u16 smid_task, ulong timeout)
1803{
1804 Mpi2SCSITaskManagementRequest_t *mpi_request;
1805 Mpi2SCSITaskManagementReply_t *mpi_reply;
1806 u16 smid = 0;
1807 u32 ioc_state;
1808 unsigned long timeleft;
Eric Moore635374e2009-03-09 01:21:12 -06001809
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301810 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1811 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1812 __func__, ioc->name);
1813 return;
1814 }
1815
1816 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06001817 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1818 __func__, ioc->name);
1819 return;
1820 }
Eric Moore635374e2009-03-09 01:21:12 -06001821
1822 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1823 if (ioc_state & MPI2_DOORBELL_USED) {
1824 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1825 "active!\n", ioc->name));
1826 goto issue_host_reset;
1827 }
1828
1829 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1830 mpt2sas_base_fault_info(ioc, ioc_state &
1831 MPI2_DOORBELL_DATA_MASK);
1832 goto issue_host_reset;
1833 }
1834
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301835 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06001836 if (!smid) {
1837 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1838 ioc->name, __func__);
1839 return;
1840 }
1841
1842 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301843 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
1844 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06001845 ioc->tm_cmds.status = MPT2_CMD_PENDING;
1846 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1847 ioc->tm_cmds.smid = smid;
1848 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1849 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1850 mpi_request->DevHandle = cpu_to_le16(handle);
1851 mpi_request->TaskType = type;
1852 mpi_request->TaskMID = cpu_to_le16(smid_task);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301853 mpi_request->VP_ID = 0; /* TODO */
1854 mpi_request->VF_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001855 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1856 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05301857 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301858 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06001859 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1860 mpt2sas_scsih_clear_tm_flag(ioc, handle);
1861 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1862 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1863 ioc->name, __func__);
1864 _debug_dump_mf(mpi_request,
1865 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1866 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1867 goto issue_host_reset;
1868 }
1869
1870 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1871 mpi_reply = ioc->tm_cmds.reply;
1872 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1873 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1874 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1875 le32_to_cpu(mpi_reply->IOCLogInfo),
1876 le32_to_cpu(mpi_reply->TerminationCount)));
1877 if (ioc->logging_level & MPT_DEBUG_TM)
1878 _scsih_response_code(ioc, mpi_reply->ResponseCode);
1879 }
1880 return;
1881 issue_host_reset:
1882 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1883}
1884
1885/**
Eric Moored5d135b2009-05-18 13:02:08 -06001886 * _scsih_abort - eh threads main abort routine
Eric Moore635374e2009-03-09 01:21:12 -06001887 * @sdev: scsi device struct
1888 *
1889 * Returns SUCCESS if command aborted else FAILED
1890 */
1891static int
Eric Moored5d135b2009-05-18 13:02:08 -06001892_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001893{
1894 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1895 struct MPT2SAS_DEVICE *sas_device_priv_data;
1896 u16 smid;
1897 u16 handle;
1898 int r;
1899 struct scsi_cmnd *scmd_lookup;
1900
1901 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1902 ioc->name, scmd);
1903 scsi_print_command(scmd);
1904
1905 sas_device_priv_data = scmd->device->hostdata;
1906 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1907 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1908 ioc->name, scmd);
1909 scmd->result = DID_NO_CONNECT << 16;
1910 scmd->scsi_done(scmd);
1911 r = SUCCESS;
1912 goto out;
1913 }
1914
1915 /* search for the command */
1916 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1917 if (!smid) {
1918 scmd->result = DID_RESET << 16;
1919 r = SUCCESS;
1920 goto out;
1921 }
1922
1923 /* for hidden raid components and volumes this is not supported */
1924 if (sas_device_priv_data->sas_target->flags &
1925 MPT_TARGET_FLAGS_RAID_COMPONENT ||
1926 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1927 scmd->result = DID_RESET << 16;
1928 r = FAILED;
1929 goto out;
1930 }
1931
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05301932 mpt2sas_halt_firmware(ioc);
1933
Eric Moore635374e2009-03-09 01:21:12 -06001934 mutex_lock(&ioc->tm_cmds.mutex);
1935 handle = sas_device_priv_data->sas_target->handle;
1936 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1937 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1938
1939 /* sanity check - see whether command actually completed */
1940 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1941 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1942 r = FAILED;
1943 else
1944 r = SUCCESS;
1945 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1946 mutex_unlock(&ioc->tm_cmds.mutex);
1947
1948 out:
1949 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1950 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1951 return r;
1952}
1953
Eric Moore635374e2009-03-09 01:21:12 -06001954/**
Eric Moored5d135b2009-05-18 13:02:08 -06001955 * _scsih_dev_reset - eh threads main device reset routine
Eric Moore635374e2009-03-09 01:21:12 -06001956 * @sdev: scsi device struct
1957 *
1958 * Returns SUCCESS if command aborted else FAILED
1959 */
1960static int
Eric Moored5d135b2009-05-18 13:02:08 -06001961_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001962{
1963 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1964 struct MPT2SAS_DEVICE *sas_device_priv_data;
1965 struct _sas_device *sas_device;
1966 unsigned long flags;
1967 u16 handle;
1968 int r;
1969
Eric Moore993e0da2009-05-18 13:00:45 -06001970 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
Eric Moore635374e2009-03-09 01:21:12 -06001971 ioc->name, scmd);
1972 scsi_print_command(scmd);
1973
1974 sas_device_priv_data = scmd->device->hostdata;
1975 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1976 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1977 ioc->name, scmd);
1978 scmd->result = DID_NO_CONNECT << 16;
1979 scmd->scsi_done(scmd);
1980 r = SUCCESS;
1981 goto out;
1982 }
1983
1984 /* for hidden raid components obtain the volume_handle */
1985 handle = 0;
1986 if (sas_device_priv_data->sas_target->flags &
1987 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1988 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1989 sas_device = _scsih_sas_device_find_by_handle(ioc,
1990 sas_device_priv_data->sas_target->handle);
1991 if (sas_device)
1992 handle = sas_device->volume_handle;
1993 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1994 } else
1995 handle = sas_device_priv_data->sas_target->handle;
1996
1997 if (!handle) {
1998 scmd->result = DID_RESET << 16;
1999 r = FAILED;
2000 goto out;
2001 }
2002
2003 mutex_lock(&ioc->tm_cmds.mutex);
2004 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore993e0da2009-05-18 13:00:45 -06002005 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
2006 30);
2007
2008 /*
2009 * sanity check see whether all commands to this device been
2010 * completed
2011 */
2012 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
2013 scmd->device->lun, scmd->device->channel))
2014 r = FAILED;
2015 else
2016 r = SUCCESS;
2017 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2018 mutex_unlock(&ioc->tm_cmds.mutex);
2019
2020 out:
2021 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2022 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2023 return r;
2024}
2025
2026/**
Eric Moored5d135b2009-05-18 13:02:08 -06002027 * _scsih_target_reset - eh threads main target reset routine
Eric Moore993e0da2009-05-18 13:00:45 -06002028 * @sdev: scsi device struct
2029 *
2030 * Returns SUCCESS if command aborted else FAILED
2031 */
2032static int
Eric Moored5d135b2009-05-18 13:02:08 -06002033_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002034{
2035 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2036 struct MPT2SAS_DEVICE *sas_device_priv_data;
2037 struct _sas_device *sas_device;
2038 unsigned long flags;
2039 u16 handle;
2040 int r;
2041
2042 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2043 ioc->name, scmd);
2044 scsi_print_command(scmd);
2045
2046 sas_device_priv_data = scmd->device->hostdata;
2047 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2048 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2049 ioc->name, scmd);
2050 scmd->result = DID_NO_CONNECT << 16;
2051 scmd->scsi_done(scmd);
2052 r = SUCCESS;
2053 goto out;
2054 }
2055
2056 /* for hidden raid components obtain the volume_handle */
2057 handle = 0;
2058 if (sas_device_priv_data->sas_target->flags &
2059 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2060 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2061 sas_device = _scsih_sas_device_find_by_handle(ioc,
2062 sas_device_priv_data->sas_target->handle);
2063 if (sas_device)
2064 handle = sas_device->volume_handle;
2065 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2066 } else
2067 handle = sas_device_priv_data->sas_target->handle;
2068
2069 if (!handle) {
2070 scmd->result = DID_RESET << 16;
2071 r = FAILED;
2072 goto out;
2073 }
2074
2075 mutex_lock(&ioc->tm_cmds.mutex);
2076 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore635374e2009-03-09 01:21:12 -06002077 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2078
2079 /*
2080 * sanity check see whether all commands to this target been
2081 * completed
2082 */
2083 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2084 scmd->device->channel))
2085 r = FAILED;
2086 else
2087 r = SUCCESS;
2088 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2089 mutex_unlock(&ioc->tm_cmds.mutex);
2090
2091 out:
2092 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2093 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2094 return r;
2095}
2096
2097/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302098 * _scsih_host_reset - eh threads main host reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002099 * @sdev: scsi device struct
2100 *
2101 * Returns SUCCESS if command aborted else FAILED
2102 */
2103static int
Eric Moored5d135b2009-05-18 13:02:08 -06002104_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002105{
2106 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2107 int r, retval;
2108
2109 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2110 ioc->name, scmd);
2111 scsi_print_command(scmd);
2112
2113 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2114 FORCE_BIG_HAMMER);
2115 r = (retval < 0) ? FAILED : SUCCESS;
2116 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2117 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2118
2119 return r;
2120}
2121
2122/**
2123 * _scsih_fw_event_add - insert and queue up fw_event
2124 * @ioc: per adapter object
2125 * @fw_event: object describing the event
2126 * Context: This function will acquire ioc->fw_event_lock.
2127 *
2128 * This adds the firmware event object into link list, then queues it up to
2129 * be processed from user context.
2130 *
2131 * Return nothing.
2132 */
2133static void
2134_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2135{
2136 unsigned long flags;
2137
2138 if (ioc->firmware_event_thread == NULL)
2139 return;
2140
2141 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2142 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Eric Moore6f92a7a2009-04-21 15:43:33 -06002143 INIT_WORK(&fw_event->work, _firmware_event_work);
2144 queue_work(ioc->firmware_event_thread, &fw_event->work);
Eric Moore635374e2009-03-09 01:21:12 -06002145 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2146}
2147
2148/**
2149 * _scsih_fw_event_free - delete fw_event
2150 * @ioc: per adapter object
2151 * @fw_event: object describing the event
2152 * Context: This function will acquire ioc->fw_event_lock.
2153 *
2154 * This removes firmware event object from link list, frees associated memory.
2155 *
2156 * Return nothing.
2157 */
2158static void
2159_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2160 *fw_event)
2161{
2162 unsigned long flags;
2163
2164 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2165 list_del(&fw_event->list);
2166 kfree(fw_event->event_data);
2167 kfree(fw_event);
2168 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2169}
2170
2171/**
2172 * _scsih_fw_event_add - requeue an event
2173 * @ioc: per adapter object
2174 * @fw_event: object describing the event
2175 * Context: This function will acquire ioc->fw_event_lock.
2176 *
2177 * Return nothing.
2178 */
2179static void
2180_scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2181 *fw_event, unsigned long delay)
2182{
2183 unsigned long flags;
2184 if (ioc->firmware_event_thread == NULL)
2185 return;
2186
2187 spin_lock_irqsave(&ioc->fw_event_lock, flags);
Eric Moore6f92a7a2009-04-21 15:43:33 -06002188 queue_work(ioc->firmware_event_thread, &fw_event->work);
Eric Moore635374e2009-03-09 01:21:12 -06002189 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2190}
2191
2192/**
2193 * _scsih_fw_event_off - turn flag off preventing event handling
2194 * @ioc: per adapter object
2195 *
2196 * Used to prevent handling of firmware events during adapter reset
2197 * driver unload.
2198 *
2199 * Return nothing.
2200 */
2201static void
2202_scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2203{
2204 unsigned long flags;
2205
2206 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2207 ioc->fw_events_off = 1;
2208 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2209
2210}
2211
2212/**
2213 * _scsih_fw_event_on - turn flag on allowing firmware event handling
2214 * @ioc: per adapter object
2215 *
2216 * Returns nothing.
2217 */
2218static void
2219_scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2220{
2221 unsigned long flags;
2222
2223 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2224 ioc->fw_events_off = 0;
2225 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2226}
2227
2228/**
2229 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2230 * @ioc: per adapter object
2231 * @handle: device handle
2232 *
2233 * During device pull we need to appropiately set the sdev state.
2234 */
2235static void
2236_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2237{
2238 struct MPT2SAS_DEVICE *sas_device_priv_data;
2239 struct scsi_device *sdev;
2240
2241 shost_for_each_device(sdev, ioc->shost) {
2242 sas_device_priv_data = sdev->hostdata;
2243 if (!sas_device_priv_data)
2244 continue;
2245 if (!sas_device_priv_data->block)
2246 continue;
2247 if (sas_device_priv_data->sas_target->handle == handle) {
2248 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2249 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2250 "handle(0x%04x)\n", ioc->name, handle));
2251 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302252 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002253 }
2254 }
2255}
2256
2257/**
2258 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2259 * @ioc: per adapter object
2260 * @handle: device handle
2261 *
2262 * During device pull we need to appropiately set the sdev state.
2263 */
2264static void
2265_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2266{
2267 struct MPT2SAS_DEVICE *sas_device_priv_data;
2268 struct scsi_device *sdev;
2269
2270 shost_for_each_device(sdev, ioc->shost) {
2271 sas_device_priv_data = sdev->hostdata;
2272 if (!sas_device_priv_data)
2273 continue;
2274 if (sas_device_priv_data->block)
2275 continue;
2276 if (sas_device_priv_data->sas_target->handle == handle) {
2277 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2278 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2279 "handle(0x%04x)\n", ioc->name, handle));
2280 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302281 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002282 }
2283 }
2284}
2285
2286/**
2287 * _scsih_block_io_to_children_attached_to_ex
2288 * @ioc: per adapter object
2289 * @sas_expander: the sas_device object
2290 *
2291 * This routine set sdev state to SDEV_BLOCK for all devices
2292 * attached to this expander. This function called when expander is
2293 * pulled.
2294 */
2295static void
2296_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2297 struct _sas_node *sas_expander)
2298{
2299 struct _sas_port *mpt2sas_port;
2300 struct _sas_device *sas_device;
2301 struct _sas_node *expander_sibling;
2302 unsigned long flags;
2303
2304 if (!sas_expander)
2305 return;
2306
2307 list_for_each_entry(mpt2sas_port,
2308 &sas_expander->sas_port_list, port_list) {
2309 if (mpt2sas_port->remote_identify.device_type ==
2310 SAS_END_DEVICE) {
2311 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2312 sas_device =
2313 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2314 mpt2sas_port->remote_identify.sas_address);
2315 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2316 if (!sas_device)
2317 continue;
2318 _scsih_block_io_device(ioc, sas_device->handle);
2319 }
2320 }
2321
2322 list_for_each_entry(mpt2sas_port,
2323 &sas_expander->sas_port_list, port_list) {
2324
2325 if (mpt2sas_port->remote_identify.device_type ==
2326 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2327 mpt2sas_port->remote_identify.device_type ==
2328 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2329
2330 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2331 expander_sibling =
2332 mpt2sas_scsih_expander_find_by_sas_address(
2333 ioc, mpt2sas_port->remote_identify.sas_address);
2334 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2335 _scsih_block_io_to_children_attached_to_ex(ioc,
2336 expander_sibling);
2337 }
2338 }
2339}
2340
2341/**
2342 * _scsih_block_io_to_children_attached_directly
2343 * @ioc: per adapter object
2344 * @event_data: topology change event data
2345 *
2346 * This routine set sdev state to SDEV_BLOCK for all devices
2347 * direct attached during device pull.
2348 */
2349static void
2350_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2351 Mpi2EventDataSasTopologyChangeList_t *event_data)
2352{
2353 int i;
2354 u16 handle;
2355 u16 reason_code;
2356 u8 phy_number;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302357 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06002358
2359 for (i = 0; i < event_data->NumEntries; i++) {
2360 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2361 if (!handle)
2362 continue;
2363 phy_number = event_data->StartPhyNum + i;
2364 reason_code = event_data->PHY[i].PhyStatus &
2365 MPI2_EVENT_SAS_TOPO_RC_MASK;
2366 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2367 _scsih_block_io_device(ioc, handle);
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302368 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
2369 link_rate = event_data->PHY[i].LinkRate >> 4;
2370 if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
2371 _scsih_ublock_io_device(ioc, handle);
2372 }
Eric Moore635374e2009-03-09 01:21:12 -06002373 }
2374}
2375
2376/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302377 * _scsih_tm_tr_send - send task management request
2378 * @ioc: per adapter object
2379 * @handle: device handle
2380 * Context: interrupt time.
2381 *
2382 * This code is to initiate the device removal handshake protocal
2383 * with controller firmware. This function will issue target reset
2384 * using high priority request queue. It will send a sas iounit
2385 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2386 *
2387 * This is designed to send muliple task management request at the same
2388 * time to the fifo. If the fifo is full, we will append the request,
2389 * and process it in a future completion.
2390 */
2391static void
2392_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2393{
2394 Mpi2SCSITaskManagementRequest_t *mpi_request;
2395 struct MPT2SAS_TARGET *sas_target_priv_data;
2396 u16 smid;
2397 struct _sas_device *sas_device;
2398 unsigned long flags;
2399 struct _tr_list *delayed_tr;
2400
2401 if (ioc->shost_recovery) {
2402 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2403 __func__, ioc->name);
2404 return;
2405 }
2406
2407 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2408 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302409 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2410
2411 /* skip is hidden raid component */
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302412 if (sas_device && sas_device->hidden_raid_component)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302413 return;
2414
2415 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2416 if (!smid) {
2417 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2418 if (!delayed_tr)
2419 return;
2420 INIT_LIST_HEAD(&delayed_tr->list);
2421 delayed_tr->handle = handle;
2422 delayed_tr->state = MPT2SAS_REQ_SAS_CNTRL;
2423 list_add_tail(&delayed_tr->list,
2424 &ioc->delayed_tr_list);
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302425 if (sas_device && sas_device->starget) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302426 dewtprintk(ioc, starget_printk(KERN_INFO,
2427 sas_device->starget, "DELAYED:tr:handle(0x%04x), "
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302428 "(open)\n", handle));
2429 } else {
2430 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2431 "DELAYED:tr:handle(0x%04x), (open)\n",
2432 ioc->name, handle));
2433 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302434 return;
2435 }
2436
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302437 if (sas_device) {
2438 sas_device->state |= MPTSAS_STATE_TR_SEND;
2439 sas_device->state |= MPT2SAS_REQ_SAS_CNTRL;
2440 if (sas_device->starget && sas_device->starget->hostdata) {
2441 sas_target_priv_data = sas_device->starget->hostdata;
2442 sas_target_priv_data->tm_busy = 1;
2443 dewtprintk(ioc, starget_printk(KERN_INFO,
2444 sas_device->starget, "tr:handle(0x%04x), (open)\n",
2445 handle));
2446 }
2447 } else {
2448 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2449 "tr:handle(0x%04x), (open)\n", ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302450 }
2451
2452 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2453 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2454 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2455 mpi_request->DevHandle = cpu_to_le16(handle);
2456 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302457 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2458}
2459
2460
2461
2462/**
2463 * _scsih_sas_control_complete - completion routine
2464 * @ioc: per adapter object
2465 * @smid: system request message index
2466 * @msix_index: MSIX table index supplied by the OS
2467 * @reply: reply message frame(lower 32bit addr)
2468 * Context: interrupt time.
2469 *
2470 * This is the sas iounit controll completion routine.
2471 * This code is part of the code to initiate the device removal
2472 * handshake protocal with controller firmware.
2473 *
2474 * Return 1 meaning mf should be freed from _base_interrupt
2475 * 0 means the mf is freed from this function.
2476 */
2477static u8
2478_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2479 u8 msix_index, u32 reply)
2480{
2481 unsigned long flags;
2482 u16 handle;
2483 struct _sas_device *sas_device;
2484 Mpi2SasIoUnitControlReply_t *mpi_reply =
2485 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2486
2487 handle = le16_to_cpu(mpi_reply->DevHandle);
2488
2489 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2490 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302491 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2492
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302493 if (sas_device) {
2494 sas_device->state |= MPTSAS_STATE_CNTRL_COMPLETE;
2495 if (sas_device->starget)
2496 dewtprintk(ioc, starget_printk(KERN_INFO,
2497 sas_device->starget,
2498 "sc_complete:handle(0x%04x), "
2499 "ioc_status(0x%04x), loginfo(0x%08x)\n",
2500 handle, le16_to_cpu(mpi_reply->IOCStatus),
2501 le32_to_cpu(mpi_reply->IOCLogInfo)));
2502 } else {
2503 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302504 "sc_complete:handle(0x%04x), "
2505 "ioc_status(0x%04x), loginfo(0x%08x)\n",
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302506 ioc->name, handle, le16_to_cpu(mpi_reply->IOCStatus),
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302507 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302508 }
2509
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302510 return 1;
2511}
2512
2513/**
2514 * _scsih_tm_tr_complete -
2515 * @ioc: per adapter object
2516 * @smid: system request message index
2517 * @msix_index: MSIX table index supplied by the OS
2518 * @reply: reply message frame(lower 32bit addr)
2519 * Context: interrupt time.
2520 *
2521 * This is the target reset completion routine.
2522 * This code is part of the code to initiate the device removal
2523 * handshake protocal with controller firmware.
2524 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2525 *
2526 * Return 1 meaning mf should be freed from _base_interrupt
2527 * 0 means the mf is freed from this function.
2528 */
2529static u8
2530_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2531 u32 reply)
2532{
2533 unsigned long flags;
2534 u16 handle;
2535 struct _sas_device *sas_device;
2536 Mpi2SCSITaskManagementReply_t *mpi_reply =
2537 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2538 Mpi2SasIoUnitControlRequest_t *mpi_request;
2539 u16 smid_sas_ctrl;
2540 struct MPT2SAS_TARGET *sas_target_priv_data;
2541 struct _tr_list *delayed_tr;
2542 u8 rc;
2543
2544 handle = le16_to_cpu(mpi_reply->DevHandle);
2545 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2546 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302547 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2548
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302549 if (sas_device) {
2550 sas_device->state |= MPTSAS_STATE_TR_COMPLETE;
2551 if (sas_device->starget) {
2552 dewtprintk(ioc, starget_printk(KERN_INFO,
2553 sas_device->starget, "tr_complete:handle(0x%04x), "
2554 "(%s) ioc_status(0x%04x), loginfo(0x%08x), "
2555 "completed(%d)\n", sas_device->handle,
2556 (sas_device->state & MPT2SAS_REQ_SAS_CNTRL) ?
2557 "open" : "active",
2558 le16_to_cpu(mpi_reply->IOCStatus),
2559 le32_to_cpu(mpi_reply->IOCLogInfo),
2560 le32_to_cpu(mpi_reply->TerminationCount)));
2561 if (sas_device->starget->hostdata) {
2562 sas_target_priv_data =
2563 sas_device->starget->hostdata;
2564 sas_target_priv_data->tm_busy = 0;
2565 }
2566 }
2567 } else {
2568 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2569 "tr_complete:handle(0x%04x), (open) ioc_status(0x%04x), "
2570 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2571 handle, le16_to_cpu(mpi_reply->IOCStatus),
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302572 le32_to_cpu(mpi_reply->IOCLogInfo),
2573 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302574 }
2575
2576 if (!list_empty(&ioc->delayed_tr_list)) {
2577 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2578 struct _tr_list, list);
2579 mpt2sas_base_free_smid(ioc, smid);
2580 if (delayed_tr->state & MPT2SAS_REQ_SAS_CNTRL)
2581 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2582 list_del(&delayed_tr->list);
2583 kfree(delayed_tr);
2584 rc = 0; /* tells base_interrupt not to free mf */
2585 } else
2586 rc = 1;
2587
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302588 if (sas_device && !(sas_device->state & MPT2SAS_REQ_SAS_CNTRL))
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302589 return rc;
2590
2591 if (ioc->shost_recovery) {
2592 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2593 __func__, ioc->name);
2594 return rc;
2595 }
2596
2597 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2598 if (!smid_sas_ctrl) {
2599 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2600 ioc->name, __func__);
2601 return rc;
2602 }
2603
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302604 if (sas_device)
2605 sas_device->state |= MPTSAS_STATE_CNTRL_SEND;
2606
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302607 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2608 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2609 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2610 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2611 mpi_request->DevHandle = mpi_reply->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302612 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2613 return rc;
2614}
2615
2616/**
Eric Moore635374e2009-03-09 01:21:12 -06002617 * _scsih_check_topo_delete_events - sanity check on topo events
2618 * @ioc: per adapter object
2619 * @event_data: the event data payload
2620 *
2621 * This routine added to better handle cable breaker.
2622 *
2623 * This handles the case where driver recieves multiple expander
2624 * add and delete events in a single shot. When there is a delete event
2625 * the routine will void any pending add events waiting in the event queue.
2626 *
2627 * Return nothing.
2628 */
2629static void
2630_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2631 Mpi2EventDataSasTopologyChangeList_t *event_data)
2632{
2633 struct fw_event_work *fw_event;
2634 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2635 u16 expander_handle;
2636 struct _sas_node *sas_expander;
2637 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302638 int i, reason_code;
2639 u16 handle;
2640
2641 for (i = 0 ; i < event_data->NumEntries; i++) {
2642 if (event_data->PHY[i].PhyStatus &
2643 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2644 continue;
2645 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2646 if (!handle)
2647 continue;
2648 reason_code = event_data->PHY[i].PhyStatus &
2649 MPI2_EVENT_SAS_TOPO_RC_MASK;
2650 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2651 _scsih_tm_tr_send(ioc, handle);
2652 }
Eric Moore635374e2009-03-09 01:21:12 -06002653
2654 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2655 if (expander_handle < ioc->sas_hba.num_phys) {
2656 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2657 return;
2658 }
2659
2660 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2661 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2662 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2663 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2664 expander_handle);
2665 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2666 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2667 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2668 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2669
2670 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2671 return;
2672
2673 /* mark ignore flag for pending events */
2674 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2675 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2676 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2677 fw_event->ignore)
2678 continue;
2679 local_event_data = fw_event->event_data;
2680 if (local_event_data->ExpStatus ==
2681 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2682 local_event_data->ExpStatus ==
2683 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2684 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2685 expander_handle) {
2686 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2687 "setting ignoring flag\n", ioc->name));
2688 fw_event->ignore = 1;
2689 }
2690 }
2691 }
2692 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2693}
2694
2695/**
Eric Moore635374e2009-03-09 01:21:12 -06002696 * _scsih_flush_running_cmds - completing outstanding commands.
2697 * @ioc: per adapter object
2698 *
2699 * The flushing out of all pending scmd commands following host reset,
2700 * where all IO is dropped to the floor.
2701 *
2702 * Return nothing.
2703 */
2704static void
2705_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2706{
2707 struct scsi_cmnd *scmd;
2708 u16 smid;
2709 u16 count = 0;
2710
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302711 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
2712 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002713 if (!scmd)
2714 continue;
2715 count++;
2716 mpt2sas_base_free_smid(ioc, smid);
2717 scsi_dma_unmap(scmd);
2718 scmd->result = DID_RESET << 16;
2719 scmd->scsi_done(scmd);
2720 }
2721 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2722 ioc->name, count));
2723}
2724
2725/**
Eric Moore3c621b32009-05-18 12:59:41 -06002726 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2727 * @scmd: pointer to scsi command object
2728 * @mpi_request: pointer to the SCSI_IO reqest message frame
2729 *
2730 * Supporting protection 1 and 3.
2731 *
2732 * Returns nothing
2733 */
2734static void
2735_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2736{
2737 u16 eedp_flags;
2738 unsigned char prot_op = scsi_get_prot_op(scmd);
2739 unsigned char prot_type = scsi_get_prot_type(scmd);
2740
2741 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2742 prot_type == SCSI_PROT_DIF_TYPE2 ||
2743 prot_op == SCSI_PROT_NORMAL)
2744 return;
2745
2746 if (prot_op == SCSI_PROT_READ_STRIP)
2747 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2748 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2749 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2750 else
2751 return;
2752
2753 mpi_request->EEDPBlockSize = scmd->device->sector_size;
2754
2755 switch (prot_type) {
2756 case SCSI_PROT_DIF_TYPE1:
2757
2758 /*
2759 * enable ref/guard checking
2760 * auto increment ref tag
2761 */
2762 mpi_request->EEDPFlags = eedp_flags |
2763 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2764 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2765 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2766 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2767 cpu_to_be32(scsi_get_lba(scmd));
2768
2769 break;
2770
2771 case SCSI_PROT_DIF_TYPE3:
2772
2773 /*
2774 * enable guard checking
2775 */
2776 mpi_request->EEDPFlags = eedp_flags |
2777 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2778
2779 break;
2780 }
2781}
2782
2783/**
2784 * _scsih_eedp_error_handling - return sense code for EEDP errors
2785 * @scmd: pointer to scsi command object
2786 * @ioc_status: ioc status
2787 *
2788 * Returns nothing
2789 */
2790static void
2791_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2792{
2793 u8 ascq;
2794 u8 sk;
2795 u8 host_byte;
2796
2797 switch (ioc_status) {
2798 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2799 ascq = 0x01;
2800 break;
2801 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2802 ascq = 0x02;
2803 break;
2804 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2805 ascq = 0x03;
2806 break;
2807 default:
2808 ascq = 0x00;
2809 break;
2810 }
2811
2812 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2813 sk = ILLEGAL_REQUEST;
2814 host_byte = DID_ABORT;
2815 } else {
2816 sk = ABORTED_COMMAND;
2817 host_byte = DID_OK;
2818 }
2819
2820 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2821 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2822 SAM_STAT_CHECK_CONDITION;
2823}
2824
2825/**
Eric Moored5d135b2009-05-18 13:02:08 -06002826 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06002827 * @scmd: pointer to scsi command object
2828 * @done: function pointer to be invoked on completion
2829 *
2830 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2831 *
2832 * Returns 0 on success. If there's a failure, return either:
2833 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2834 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2835 */
2836static int
Eric Moored5d135b2009-05-18 13:02:08 -06002837_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06002838{
2839 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2840 struct MPT2SAS_DEVICE *sas_device_priv_data;
2841 struct MPT2SAS_TARGET *sas_target_priv_data;
2842 Mpi2SCSIIORequest_t *mpi_request;
2843 u32 mpi_control;
2844 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06002845
2846 scmd->scsi_done = done;
2847 sas_device_priv_data = scmd->device->hostdata;
2848 if (!sas_device_priv_data) {
2849 scmd->result = DID_NO_CONNECT << 16;
2850 scmd->scsi_done(scmd);
2851 return 0;
2852 }
2853
2854 sas_target_priv_data = sas_device_priv_data->sas_target;
2855 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2856 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2857 scmd->result = DID_NO_CONNECT << 16;
2858 scmd->scsi_done(scmd);
2859 return 0;
2860 }
2861
2862 /* see if we are busy with task managment stuff */
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302863 if (sas_target_priv_data->tm_busy)
2864 return SCSI_MLQUEUE_DEVICE_BUSY;
2865 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06002866 return SCSI_MLQUEUE_HOST_BUSY;
Eric Moore635374e2009-03-09 01:21:12 -06002867
2868 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2869 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2870 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2871 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2872 else
2873 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2874
2875 /* set tags */
2876 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2877 if (scmd->device->tagged_supported) {
2878 if (scmd->device->ordered_tags)
2879 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2880 else
2881 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2882 } else
2883/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2884/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2885 */
2886 mpi_control |= (0x500);
2887
2888 } else
2889 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2890
2891 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2892 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2893
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302894 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002895 if (!smid) {
2896 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2897 ioc->name, __func__);
2898 goto out;
2899 }
2900 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2901 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06002902 _scsih_setup_eedp(scmd, mpi_request);
Eric Moore635374e2009-03-09 01:21:12 -06002903 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2904 if (sas_device_priv_data->sas_target->flags &
2905 MPT_TARGET_FLAGS_RAID_COMPONENT)
2906 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2907 else
2908 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2909 mpi_request->DevHandle =
2910 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2911 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2912 mpi_request->Control = cpu_to_le32(mpi_control);
2913 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2914 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2915 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2916 mpi_request->SenseBufferLowAddress =
2917 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2918 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2919 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2920 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302921 mpi_request->VF_ID = 0; /* TODO */
2922 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002923 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2924 mpi_request->LUN);
2925 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2926
2927 if (!mpi_request->DataLength) {
2928 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2929 } else {
2930 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2931 mpt2sas_base_free_smid(ioc, smid);
2932 goto out;
2933 }
2934 }
2935
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302936 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Eric Moore635374e2009-03-09 01:21:12 -06002937 sas_device_priv_data->sas_target->handle);
2938 return 0;
2939
2940 out:
2941 return SCSI_MLQUEUE_HOST_BUSY;
2942}
2943
2944/**
2945 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2946 * @sense_buffer: sense data returned by target
2947 * @data: normalized skey/asc/ascq
2948 *
2949 * Return nothing.
2950 */
2951static void
2952_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2953{
2954 if ((sense_buffer[0] & 0x7F) >= 0x72) {
2955 /* descriptor format */
2956 data->skey = sense_buffer[1] & 0x0F;
2957 data->asc = sense_buffer[2];
2958 data->ascq = sense_buffer[3];
2959 } else {
2960 /* fixed format */
2961 data->skey = sense_buffer[2] & 0x0F;
2962 data->asc = sense_buffer[12];
2963 data->ascq = sense_buffer[13];
2964 }
2965}
2966
2967#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2968/**
2969 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2970 * @ioc: per adapter object
2971 * @scmd: pointer to scsi command object
2972 * @mpi_reply: reply mf payload returned from firmware
2973 *
2974 * scsi_status - SCSI Status code returned from target device
2975 * scsi_state - state info associated with SCSI_IO determined by ioc
2976 * ioc_status - ioc supplied status info
2977 *
2978 * Return nothing.
2979 */
2980static void
2981_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2982 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2983{
2984 u32 response_info;
2985 u8 *response_bytes;
2986 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2987 MPI2_IOCSTATUS_MASK;
2988 u8 scsi_state = mpi_reply->SCSIState;
2989 u8 scsi_status = mpi_reply->SCSIStatus;
2990 char *desc_ioc_state = NULL;
2991 char *desc_scsi_status = NULL;
2992 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05302993 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
2994
2995 if (log_info == 0x31170000)
2996 return;
Eric Moore635374e2009-03-09 01:21:12 -06002997
2998 switch (ioc_status) {
2999 case MPI2_IOCSTATUS_SUCCESS:
3000 desc_ioc_state = "success";
3001 break;
3002 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3003 desc_ioc_state = "invalid function";
3004 break;
3005 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3006 desc_ioc_state = "scsi recovered error";
3007 break;
3008 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3009 desc_ioc_state = "scsi invalid dev handle";
3010 break;
3011 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3012 desc_ioc_state = "scsi device not there";
3013 break;
3014 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3015 desc_ioc_state = "scsi data overrun";
3016 break;
3017 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3018 desc_ioc_state = "scsi data underrun";
3019 break;
3020 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3021 desc_ioc_state = "scsi io data error";
3022 break;
3023 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3024 desc_ioc_state = "scsi protocol error";
3025 break;
3026 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3027 desc_ioc_state = "scsi task terminated";
3028 break;
3029 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3030 desc_ioc_state = "scsi residual mismatch";
3031 break;
3032 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3033 desc_ioc_state = "scsi task mgmt failed";
3034 break;
3035 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3036 desc_ioc_state = "scsi ioc terminated";
3037 break;
3038 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3039 desc_ioc_state = "scsi ext terminated";
3040 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003041 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3042 desc_ioc_state = "eedp guard error";
3043 break;
3044 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3045 desc_ioc_state = "eedp ref tag error";
3046 break;
3047 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3048 desc_ioc_state = "eedp app tag error";
3049 break;
Eric Moore635374e2009-03-09 01:21:12 -06003050 default:
3051 desc_ioc_state = "unknown";
3052 break;
3053 }
3054
3055 switch (scsi_status) {
3056 case MPI2_SCSI_STATUS_GOOD:
3057 desc_scsi_status = "good";
3058 break;
3059 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3060 desc_scsi_status = "check condition";
3061 break;
3062 case MPI2_SCSI_STATUS_CONDITION_MET:
3063 desc_scsi_status = "condition met";
3064 break;
3065 case MPI2_SCSI_STATUS_BUSY:
3066 desc_scsi_status = "busy";
3067 break;
3068 case MPI2_SCSI_STATUS_INTERMEDIATE:
3069 desc_scsi_status = "intermediate";
3070 break;
3071 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3072 desc_scsi_status = "intermediate condmet";
3073 break;
3074 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3075 desc_scsi_status = "reservation conflict";
3076 break;
3077 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3078 desc_scsi_status = "command terminated";
3079 break;
3080 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3081 desc_scsi_status = "task set full";
3082 break;
3083 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3084 desc_scsi_status = "aca active";
3085 break;
3086 case MPI2_SCSI_STATUS_TASK_ABORTED:
3087 desc_scsi_status = "task aborted";
3088 break;
3089 default:
3090 desc_scsi_status = "unknown";
3091 break;
3092 }
3093
3094 desc_scsi_state[0] = '\0';
3095 if (!scsi_state)
3096 desc_scsi_state = " ";
3097 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3098 strcat(desc_scsi_state, "response info ");
3099 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3100 strcat(desc_scsi_state, "state terminated ");
3101 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3102 strcat(desc_scsi_state, "no status ");
3103 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3104 strcat(desc_scsi_state, "autosense failed ");
3105 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3106 strcat(desc_scsi_state, "autosense valid ");
3107
3108 scsi_print_command(scmd);
3109 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3110 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3111 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3112 ioc_status, smid);
3113 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3114 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3115 scsi_get_resid(scmd));
3116 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3117 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3118 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3119 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3120 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3121 scsi_status, desc_scsi_state, scsi_state);
3122
3123 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3124 struct sense_info data;
3125 _scsih_normalize_sense(scmd->sense_buffer, &data);
3126 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3127 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
3128 data.asc, data.ascq);
3129 }
3130
3131 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3132 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3133 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303134 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003135 }
3136}
3137#endif
3138
3139/**
3140 * _scsih_smart_predicted_fault - illuminate Fault LED
3141 * @ioc: per adapter object
3142 * @handle: device handle
3143 *
3144 * Return nothing.
3145 */
3146static void
3147_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3148{
3149 Mpi2SepReply_t mpi_reply;
3150 Mpi2SepRequest_t mpi_request;
3151 struct scsi_target *starget;
3152 struct MPT2SAS_TARGET *sas_target_priv_data;
3153 Mpi2EventNotificationReply_t *event_reply;
3154 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3155 struct _sas_device *sas_device;
3156 ssize_t sz;
3157 unsigned long flags;
3158
3159 /* only handle non-raid devices */
3160 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3161 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3162 if (!sas_device) {
3163 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3164 return;
3165 }
3166 starget = sas_device->starget;
3167 sas_target_priv_data = starget->hostdata;
3168
3169 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3170 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3171 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3172 return;
3173 }
3174 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3175 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3176
3177 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3178 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3179 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3180 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3181 mpi_request.SlotStatus =
3182 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
3183 mpi_request.DevHandle = cpu_to_le16(handle);
3184 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3185 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3186 &mpi_request)) != 0) {
3187 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3188 ioc->name, __FILE__, __LINE__, __func__);
3189 return;
3190 }
3191
3192 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3193 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3194 "enclosure_processor: ioc_status (0x%04x), "
3195 "loginfo(0x%08x)\n", ioc->name,
3196 le16_to_cpu(mpi_reply.IOCStatus),
3197 le32_to_cpu(mpi_reply.IOCLogInfo)));
3198 return;
3199 }
3200 }
3201
3202 /* insert into event log */
3203 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3204 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3205 event_reply = kzalloc(sz, GFP_KERNEL);
3206 if (!event_reply) {
3207 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3208 ioc->name, __FILE__, __LINE__, __func__);
3209 return;
3210 }
3211
3212 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3213 event_reply->Event =
3214 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3215 event_reply->MsgLength = sz/4;
3216 event_reply->EventDataLength =
3217 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3218 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3219 event_reply->EventData;
3220 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3221 event_data->ASC = 0x5D;
3222 event_data->DevHandle = cpu_to_le16(handle);
3223 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3224 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3225 kfree(event_reply);
3226}
3227
3228/**
Eric Moored5d135b2009-05-18 13:02:08 -06003229 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003230 * @ioc: per adapter object
3231 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303232 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003233 * @reply: reply message frame(lower 32bit addr)
3234 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303235 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003236 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303237 * Return 1 meaning mf should be freed from _base_interrupt
3238 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003239 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303240static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303241_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003242{
3243 Mpi2SCSIIORequest_t *mpi_request;
3244 Mpi2SCSIIOReply_t *mpi_reply;
3245 struct scsi_cmnd *scmd;
3246 u16 ioc_status;
3247 u32 xfer_cnt;
3248 u8 scsi_state;
3249 u8 scsi_status;
3250 u32 log_info;
3251 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303252 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003253
3254 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303255 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003256 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303257 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003258
3259 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3260
3261 if (mpi_reply == NULL) {
3262 scmd->result = DID_OK << 16;
3263 goto out;
3264 }
3265
3266 sas_device_priv_data = scmd->device->hostdata;
3267 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3268 sas_device_priv_data->sas_target->deleted) {
3269 scmd->result = DID_NO_CONNECT << 16;
3270 goto out;
3271 }
3272
3273 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303274 scsi_state = mpi_reply->SCSIState;
3275 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3276 response_code =
3277 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003278 if (!sas_device_priv_data->tlr_snoop_check) {
3279 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303280 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
3281 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
3282 sas_device_priv_data->flags &=
3283 ~MPT_DEVICE_TLR_ON;
Eric Moore635374e2009-03-09 01:21:12 -06003284 }
3285
3286 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3287 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3288 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3289 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3290 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3291 else
3292 log_info = 0;
3293 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003294 scsi_status = mpi_reply->SCSIStatus;
3295
3296 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3297 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3298 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3299 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3300 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3301 }
3302
3303 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3304 struct sense_info data;
3305 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3306 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003307 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003308 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003309 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003310 _scsih_normalize_sense(scmd->sense_buffer, &data);
3311 /* failure prediction threshold exceeded */
3312 if (data.asc == 0x5D)
3313 _scsih_smart_predicted_fault(ioc,
3314 le16_to_cpu(mpi_reply->DevHandle));
3315 }
3316
3317 switch (ioc_status) {
3318 case MPI2_IOCSTATUS_BUSY:
3319 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3320 scmd->result = SAM_STAT_BUSY;
3321 break;
3322
3323 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3324 scmd->result = DID_NO_CONNECT << 16;
3325 break;
3326
3327 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3328 if (sas_device_priv_data->block) {
3329 scmd->result = (DID_BUS_BUSY << 16);
3330 break;
3331 }
3332
3333 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3334 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3335 scmd->result = DID_RESET << 16;
3336 break;
3337
3338 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3339 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3340 scmd->result = DID_SOFT_ERROR << 16;
3341 else
3342 scmd->result = (DID_OK << 16) | scsi_status;
3343 break;
3344
3345 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3346 scmd->result = (DID_OK << 16) | scsi_status;
3347
3348 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3349 break;
3350
3351 if (xfer_cnt < scmd->underflow) {
3352 if (scsi_status == SAM_STAT_BUSY)
3353 scmd->result = SAM_STAT_BUSY;
3354 else
3355 scmd->result = DID_SOFT_ERROR << 16;
3356 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3357 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3358 scmd->result = DID_SOFT_ERROR << 16;
3359 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3360 scmd->result = DID_RESET << 16;
3361 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3362 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3363 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3364 scmd->result = (DRIVER_SENSE << 24) |
3365 SAM_STAT_CHECK_CONDITION;
3366 scmd->sense_buffer[0] = 0x70;
3367 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3368 scmd->sense_buffer[12] = 0x20;
3369 scmd->sense_buffer[13] = 0;
3370 }
3371 break;
3372
3373 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3374 scsi_set_resid(scmd, 0);
3375 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3376 case MPI2_IOCSTATUS_SUCCESS:
3377 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303378 if (response_code ==
3379 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3380 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3381 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003382 scmd->result = DID_SOFT_ERROR << 16;
3383 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3384 scmd->result = DID_RESET << 16;
3385 break;
3386
Eric Moore3c621b32009-05-18 12:59:41 -06003387 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3388 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3389 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3390 _scsih_eedp_error_handling(scmd, ioc_status);
3391 break;
Eric Moore635374e2009-03-09 01:21:12 -06003392 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3393 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3394 case MPI2_IOCSTATUS_INVALID_SGL:
3395 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3396 case MPI2_IOCSTATUS_INVALID_FIELD:
3397 case MPI2_IOCSTATUS_INVALID_STATE:
3398 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3399 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3400 default:
3401 scmd->result = DID_SOFT_ERROR << 16;
3402 break;
3403
3404 }
3405
3406#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3407 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3408 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3409#endif
3410
3411 out:
3412 scsi_dma_unmap(scmd);
3413 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303414 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003415}
3416
3417/**
Eric Moore635374e2009-03-09 01:21:12 -06003418 * _scsih_sas_host_refresh - refreshing sas host object contents
3419 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003420 * Context: user
3421 *
3422 * During port enable, fw will send topology events for every device. Its
3423 * possible that the handles may change from the previous setting, so this
3424 * code keeping handles updating if changed.
3425 *
3426 * Return nothing.
3427 */
3428static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303429_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06003430{
3431 u16 sz;
3432 u16 ioc_status;
3433 int i;
3434 Mpi2ConfigReply_t mpi_reply;
3435 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303436 u16 attached_handle;
Eric Moore635374e2009-03-09 01:21:12 -06003437
3438 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3439 "updating handles for sas_host(0x%016llx)\n",
3440 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3441
3442 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3443 * sizeof(Mpi2SasIOUnit0PhyData_t));
3444 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3445 if (!sas_iounit_pg0) {
3446 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3447 ioc->name, __FILE__, __LINE__, __func__);
3448 return;
3449 }
Eric Moore635374e2009-03-09 01:21:12 -06003450
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303451 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3452 sas_iounit_pg0, sz)) != 0)
3453 goto out;
3454 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3455 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3456 goto out;
3457 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3458 if (i == 0)
3459 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3460 PhyData[0].ControllerDevHandle);
3461 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3462 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3463 AttachedDevHandle);
3464 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3465 attached_handle, i, sas_iounit_pg0->PhyData[i].
3466 NegotiatedLinkRate >> 4);
3467 }
Eric Moore635374e2009-03-09 01:21:12 -06003468 out:
3469 kfree(sas_iounit_pg0);
3470}
3471
3472/**
3473 * _scsih_sas_host_add - create sas host object
3474 * @ioc: per adapter object
3475 *
3476 * Creating host side data object, stored in ioc->sas_hba
3477 *
3478 * Return nothing.
3479 */
3480static void
3481_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3482{
3483 int i;
3484 Mpi2ConfigReply_t mpi_reply;
3485 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3486 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3487 Mpi2SasPhyPage0_t phy_pg0;
3488 Mpi2SasDevicePage0_t sas_device_pg0;
3489 Mpi2SasEnclosurePage0_t enclosure_pg0;
3490 u16 ioc_status;
3491 u16 sz;
3492 u16 device_missing_delay;
3493
3494 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3495 if (!ioc->sas_hba.num_phys) {
3496 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3497 ioc->name, __FILE__, __LINE__, __func__);
3498 return;
3499 }
3500
3501 /* sas_iounit page 0 */
3502 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3503 sizeof(Mpi2SasIOUnit0PhyData_t));
3504 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3505 if (!sas_iounit_pg0) {
3506 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3507 ioc->name, __FILE__, __LINE__, __func__);
3508 return;
3509 }
3510 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3511 sas_iounit_pg0, sz))) {
3512 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3513 ioc->name, __FILE__, __LINE__, __func__);
3514 goto out;
3515 }
3516 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3517 MPI2_IOCSTATUS_MASK;
3518 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3519 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3520 ioc->name, __FILE__, __LINE__, __func__);
3521 goto out;
3522 }
3523
3524 /* sas_iounit page 1 */
3525 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3526 sizeof(Mpi2SasIOUnit1PhyData_t));
3527 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3528 if (!sas_iounit_pg1) {
3529 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3530 ioc->name, __FILE__, __LINE__, __func__);
3531 goto out;
3532 }
3533 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3534 sas_iounit_pg1, sz))) {
3535 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3536 ioc->name, __FILE__, __LINE__, __func__);
3537 goto out;
3538 }
3539 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3540 MPI2_IOCSTATUS_MASK;
3541 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3542 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3543 ioc->name, __FILE__, __LINE__, __func__);
3544 goto out;
3545 }
3546
3547 ioc->io_missing_delay =
3548 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3549 device_missing_delay =
3550 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3551 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3552 ioc->device_missing_delay = (device_missing_delay &
3553 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3554 else
3555 ioc->device_missing_delay = device_missing_delay &
3556 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3557
3558 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3559 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3560 sizeof(struct _sas_phy), GFP_KERNEL);
3561 if (!ioc->sas_hba.phy) {
3562 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3563 ioc->name, __FILE__, __LINE__, __func__);
3564 goto out;
3565 }
3566 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3567 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3568 i))) {
3569 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3570 ioc->name, __FILE__, __LINE__, __func__);
3571 goto out;
3572 }
3573 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3574 MPI2_IOCSTATUS_MASK;
3575 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3576 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3577 ioc->name, __FILE__, __LINE__, __func__);
3578 goto out;
3579 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303580
3581 if (i == 0)
3582 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3583 PhyData[0].ControllerDevHandle);
3584 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06003585 ioc->sas_hba.phy[i].phy_id = i;
3586 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3587 phy_pg0, ioc->sas_hba.parent_dev);
3588 }
3589 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303590 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06003591 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3592 ioc->name, __FILE__, __LINE__, __func__);
3593 goto out;
3594 }
Eric Moore635374e2009-03-09 01:21:12 -06003595 ioc->sas_hba.enclosure_handle =
3596 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3597 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3598 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3599 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3600 (unsigned long long) ioc->sas_hba.sas_address,
3601 ioc->sas_hba.num_phys) ;
3602
3603 if (ioc->sas_hba.enclosure_handle) {
3604 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3605 &enclosure_pg0,
3606 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3607 ioc->sas_hba.enclosure_handle))) {
3608 ioc->sas_hba.enclosure_logical_id =
3609 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3610 }
3611 }
3612
3613 out:
3614 kfree(sas_iounit_pg1);
3615 kfree(sas_iounit_pg0);
3616}
3617
3618/**
3619 * _scsih_expander_add - creating expander object
3620 * @ioc: per adapter object
3621 * @handle: expander handle
3622 *
3623 * Creating expander object, stored in ioc->sas_expander_list.
3624 *
3625 * Return 0 for success, else error.
3626 */
3627static int
3628_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3629{
3630 struct _sas_node *sas_expander;
3631 Mpi2ConfigReply_t mpi_reply;
3632 Mpi2ExpanderPage0_t expander_pg0;
3633 Mpi2ExpanderPage1_t expander_pg1;
3634 Mpi2SasEnclosurePage0_t enclosure_pg0;
3635 u32 ioc_status;
3636 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303637 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003638 int i;
3639 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303640 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003641 int rc = 0;
3642
3643 if (!handle)
3644 return -1;
3645
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303646 if (ioc->shost_recovery)
3647 return -1;
3648
Eric Moore635374e2009-03-09 01:21:12 -06003649 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3650 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3651 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3652 ioc->name, __FILE__, __LINE__, __func__);
3653 return -1;
3654 }
3655
3656 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3657 MPI2_IOCSTATUS_MASK;
3658 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3659 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3660 ioc->name, __FILE__, __LINE__, __func__);
3661 return -1;
3662 }
3663
3664 /* handle out of order topology events */
3665 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303666 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
3667 != 0) {
3668 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3669 ioc->name, __FILE__, __LINE__, __func__);
3670 return -1;
3671 }
3672 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06003673 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303674 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3675 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003676 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3677 if (!sas_expander) {
3678 rc = _scsih_expander_add(ioc, parent_handle);
3679 if (rc != 0)
3680 return rc;
3681 }
3682 }
3683
Eric Moore635374e2009-03-09 01:21:12 -06003684 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303685 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06003686 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3687 sas_address);
3688 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3689
3690 if (sas_expander)
3691 return 0;
3692
3693 sas_expander = kzalloc(sizeof(struct _sas_node),
3694 GFP_KERNEL);
3695 if (!sas_expander) {
3696 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3697 ioc->name, __FILE__, __LINE__, __func__);
3698 return -1;
3699 }
3700
3701 sas_expander->handle = handle;
3702 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303703 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06003704 sas_expander->sas_address = sas_address;
3705
3706 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3707 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303708 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06003709 sas_expander->sas_address, sas_expander->num_phys);
3710
3711 if (!sas_expander->num_phys)
3712 goto out_fail;
3713 sas_expander->phy = kcalloc(sas_expander->num_phys,
3714 sizeof(struct _sas_phy), GFP_KERNEL);
3715 if (!sas_expander->phy) {
3716 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3717 ioc->name, __FILE__, __LINE__, __func__);
3718 rc = -1;
3719 goto out_fail;
3720 }
3721
3722 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3723 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303724 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003725 if (!mpt2sas_port) {
3726 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3727 ioc->name, __FILE__, __LINE__, __func__);
3728 rc = -1;
3729 goto out_fail;
3730 }
3731 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3732
3733 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3734 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3735 &expander_pg1, i, handle))) {
3736 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3737 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05303738 rc = -1;
3739 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06003740 }
3741 sas_expander->phy[i].handle = handle;
3742 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303743
3744 if ((mpt2sas_transport_add_expander_phy(ioc,
3745 &sas_expander->phy[i], expander_pg1,
3746 sas_expander->parent_dev))) {
3747 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3748 ioc->name, __FILE__, __LINE__, __func__);
3749 rc = -1;
3750 goto out_fail;
3751 }
Eric Moore635374e2009-03-09 01:21:12 -06003752 }
3753
3754 if (sas_expander->enclosure_handle) {
3755 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3756 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3757 sas_expander->enclosure_handle))) {
3758 sas_expander->enclosure_logical_id =
3759 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3760 }
3761 }
3762
3763 _scsih_expander_node_add(ioc, sas_expander);
3764 return 0;
3765
3766 out_fail:
3767
Kashyap, Desai20f58952009-08-07 19:34:26 +05303768 if (mpt2sas_port)
3769 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303770 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003771 kfree(sas_expander);
3772 return rc;
3773}
3774
3775/**
3776 * _scsih_expander_remove - removing expander object
3777 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303778 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06003779 *
3780 * Return nothing.
3781 */
3782static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303783_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06003784{
3785 struct _sas_node *sas_expander;
3786 unsigned long flags;
3787
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303788 if (ioc->shost_recovery)
3789 return;
3790
Eric Moore635374e2009-03-09 01:21:12 -06003791 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303792 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3793 sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06003794 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3795 _scsih_expander_node_remove(ioc, sas_expander);
3796}
3797
3798/**
3799 * _scsih_add_device - creating sas device object
3800 * @ioc: per adapter object
3801 * @handle: sas device handle
3802 * @phy_num: phy number end device attached to
3803 * @is_pd: is this hidden raid component
3804 *
3805 * Creating end device object, stored in ioc->sas_device_list.
3806 *
3807 * Returns 0 for success, non-zero for failure.
3808 */
3809static int
3810_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3811{
3812 Mpi2ConfigReply_t mpi_reply;
3813 Mpi2SasDevicePage0_t sas_device_pg0;
3814 Mpi2SasEnclosurePage0_t enclosure_pg0;
3815 struct _sas_device *sas_device;
3816 u32 ioc_status;
3817 __le64 sas_address;
3818 u32 device_info;
3819 unsigned long flags;
3820
3821 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3822 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3823 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3824 ioc->name, __FILE__, __LINE__, __func__);
3825 return -1;
3826 }
3827
3828 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3829 MPI2_IOCSTATUS_MASK;
3830 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3831 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3832 ioc->name, __FILE__, __LINE__, __func__);
3833 return -1;
3834 }
3835
3836 /* check if device is present */
3837 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3838 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3839 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3840 ioc->name, __FILE__, __LINE__, __func__);
3841 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3842 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3843 return -1;
3844 }
3845
3846 /* check if there were any issus with discovery */
3847 if (sas_device_pg0.AccessStatus ==
3848 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3849 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3850 ioc->name, __FILE__, __LINE__, __func__);
3851 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3852 ioc->name, sas_device_pg0.AccessStatus);
3853 return -1;
3854 }
3855
3856 /* check if this is end device */
3857 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3858 if (!(_scsih_is_end_device(device_info))) {
3859 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3860 ioc->name, __FILE__, __LINE__, __func__);
3861 return -1;
3862 }
3863
3864 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3865
3866 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3867 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3868 sas_address);
3869 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3870
3871 if (sas_device) {
3872 _scsih_ublock_io_device(ioc, handle);
3873 return 0;
3874 }
3875
3876 sas_device = kzalloc(sizeof(struct _sas_device),
3877 GFP_KERNEL);
3878 if (!sas_device) {
3879 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3880 ioc->name, __FILE__, __LINE__, __func__);
3881 return -1;
3882 }
3883
3884 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303885 if (_scsih_get_sas_address(ioc, le16_to_cpu
3886 (sas_device_pg0.ParentDevHandle),
3887 &sas_device->sas_address_parent) != 0)
3888 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3889 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06003890 sas_device->enclosure_handle =
3891 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3892 sas_device->slot =
3893 le16_to_cpu(sas_device_pg0.Slot);
3894 sas_device->device_info = device_info;
3895 sas_device->sas_address = sas_address;
3896 sas_device->hidden_raid_component = is_pd;
3897
3898 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05303899 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3900 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3901 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06003902 sas_device->enclosure_logical_id =
3903 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06003904
3905 /* get device name */
3906 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3907
3908 if (ioc->wait_for_port_enable_to_complete)
3909 _scsih_sas_device_init_add(ioc, sas_device);
3910 else
3911 _scsih_sas_device_add(ioc, sas_device);
3912
3913 return 0;
3914}
3915
3916/**
3917 * _scsih_remove_device - removing sas device object
3918 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303919 * @sas_device: the sas_device object
Eric Moore635374e2009-03-09 01:21:12 -06003920 *
3921 * Return nothing.
3922 */
3923static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303924_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
3925 *sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06003926{
3927 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06003928 Mpi2SasIoUnitControlReply_t mpi_reply;
3929 Mpi2SasIoUnitControlRequest_t mpi_request;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303930 u16 device_handle, handle;
Eric Moore635374e2009-03-09 01:21:12 -06003931
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303932 if (!sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06003933 return;
Eric Moore635374e2009-03-09 01:21:12 -06003934
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303935 handle = sas_device->handle;
3936 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
3937 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
3938 (unsigned long long) sas_device->sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06003939
3940 if (sas_device->starget && sas_device->starget->hostdata) {
3941 sas_target_priv_data = sas_device->starget->hostdata;
3942 sas_target_priv_data->deleted = 1;
3943 }
Eric Moore635374e2009-03-09 01:21:12 -06003944
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303945 if (ioc->remove_host || ioc->shost_recovery || !handle)
Eric Moore635374e2009-03-09 01:21:12 -06003946 goto out;
3947
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303948 if ((sas_device->state & MPTSAS_STATE_TR_COMPLETE)) {
3949 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303950 "target_reset handle(0x%04x)\n", ioc->name,
3951 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303952 goto skip_tr;
3953 }
3954
Eric Moore635374e2009-03-09 01:21:12 -06003955 /* Target Reset to flush out all the outstanding IO */
3956 device_handle = (sas_device->hidden_raid_component) ?
3957 sas_device->volume_handle : handle;
3958 if (device_handle) {
3959 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3960 "handle(0x%04x)\n", ioc->name, device_handle));
3961 mutex_lock(&ioc->tm_cmds.mutex);
3962 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3963 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3964 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3965 mutex_unlock(&ioc->tm_cmds.mutex);
3966 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3967 "done: handle(0x%04x)\n", ioc->name, device_handle));
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303968 if (ioc->shost_recovery)
3969 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003970 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303971 skip_tr:
3972
3973 if ((sas_device->state & MPTSAS_STATE_CNTRL_COMPLETE)) {
3974 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
3975 "sas_cntrl handle(0x%04x)\n", ioc->name, handle));
3976 goto out;
3977 }
Eric Moore635374e2009-03-09 01:21:12 -06003978
3979 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
3980 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
3981 "(0x%04x)\n", ioc->name, handle));
3982 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3983 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3984 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3985 mpi_request.DevHandle = handle;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303986 mpi_request.VF_ID = 0; /* TODO */
3987 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003988 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
3989 &mpi_request)) != 0) {
3990 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3991 ioc->name, __FILE__, __LINE__, __func__);
3992 }
3993
3994 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
3995 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
3996 le16_to_cpu(mpi_reply.IOCStatus),
3997 le32_to_cpu(mpi_reply.IOCLogInfo)));
3998
3999 out:
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304000
4001 _scsih_ublock_io_device(ioc, handle);
4002
Eric Moore635374e2009-03-09 01:21:12 -06004003 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304004 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004005
4006 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304007 "(0x%016llx)\n", ioc->name, handle,
Eric Moore635374e2009-03-09 01:21:12 -06004008 (unsigned long long) sas_device->sas_address);
4009 _scsih_sas_device_remove(ioc, sas_device);
4010
4011 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
4012 "(0x%04x)\n", ioc->name, __func__, handle));
4013}
4014
4015#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4016/**
4017 * _scsih_sas_topology_change_event_debug - debug for topology event
4018 * @ioc: per adapter object
4019 * @event_data: event data payload
4020 * Context: user.
4021 */
4022static void
4023_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4024 Mpi2EventDataSasTopologyChangeList_t *event_data)
4025{
4026 int i;
4027 u16 handle;
4028 u16 reason_code;
4029 u8 phy_number;
4030 char *status_str = NULL;
4031 char link_rate[25];
4032
4033 switch (event_data->ExpStatus) {
4034 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4035 status_str = "add";
4036 break;
4037 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4038 status_str = "remove";
4039 break;
4040 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
4041 status_str = "responding";
4042 break;
4043 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4044 status_str = "remove delay";
4045 break;
4046 default:
4047 status_str = "unknown status";
4048 break;
4049 }
4050 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
4051 ioc->name, status_str);
4052 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
4053 "start_phy(%02d), count(%d)\n",
4054 le16_to_cpu(event_data->ExpanderDevHandle),
4055 le16_to_cpu(event_data->EnclosureHandle),
4056 event_data->StartPhyNum, event_data->NumEntries);
4057 for (i = 0; i < event_data->NumEntries; i++) {
4058 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4059 if (!handle)
4060 continue;
4061 phy_number = event_data->StartPhyNum + i;
4062 reason_code = event_data->PHY[i].PhyStatus &
4063 MPI2_EVENT_SAS_TOPO_RC_MASK;
4064 switch (reason_code) {
4065 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4066 snprintf(link_rate, 25, ": add, link(0x%02x)",
4067 (event_data->PHY[i].LinkRate >> 4));
4068 status_str = link_rate;
4069 break;
4070 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4071 status_str = ": remove";
4072 break;
4073 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
4074 status_str = ": remove_delay";
4075 break;
4076 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4077 snprintf(link_rate, 25, ": link(0x%02x)",
4078 (event_data->PHY[i].LinkRate >> 4));
4079 status_str = link_rate;
4080 break;
4081 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
4082 status_str = ": responding";
4083 break;
4084 default:
4085 status_str = ": unknown";
4086 break;
4087 }
4088 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
4089 phy_number, handle, status_str);
4090 }
4091}
4092#endif
4093
4094/**
4095 * _scsih_sas_topology_change_event - handle topology changes
4096 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304097 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004098 * Context: user.
4099 *
4100 */
4101static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304102_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004103 struct fw_event_work *fw_event)
4104{
4105 int i;
4106 u16 parent_handle, handle;
4107 u16 reason_code;
4108 u8 phy_number;
4109 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304110 struct _sas_device *sas_device;
4111 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004112 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304113 u8 link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304114 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004115
4116#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4117 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4118 _scsih_sas_topology_change_event_debug(ioc, event_data);
4119#endif
4120
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304121 if (ioc->shost_recovery)
4122 return;
4123
Eric Moore635374e2009-03-09 01:21:12 -06004124 if (!ioc->sas_hba.num_phys)
4125 _scsih_sas_host_add(ioc);
4126 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304127 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004128
4129 if (fw_event->ignore) {
4130 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
4131 "event\n", ioc->name));
4132 return;
4133 }
4134
4135 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4136
4137 /* handle expander add */
4138 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4139 if (_scsih_expander_add(ioc, parent_handle) != 0)
4140 return;
4141
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304142 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4143 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4144 parent_handle);
4145 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4146 if (sas_expander)
4147 sas_address = sas_expander->sas_address;
4148 else if (parent_handle < ioc->sas_hba.num_phys)
4149 sas_address = ioc->sas_hba.sas_address;
4150 else
4151 return;
4152
Eric Moore635374e2009-03-09 01:21:12 -06004153 /* handle siblings events */
4154 for (i = 0; i < event_data->NumEntries; i++) {
4155 if (fw_event->ignore) {
4156 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
4157 "expander event\n", ioc->name));
4158 return;
4159 }
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304160 if (ioc->shost_recovery)
4161 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304162 phy_number = event_data->StartPhyNum + i;
4163 reason_code = event_data->PHY[i].PhyStatus &
4164 MPI2_EVENT_SAS_TOPO_RC_MASK;
4165 if ((event_data->PHY[i].PhyStatus &
4166 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4167 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004168 continue;
4169 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4170 if (!handle)
4171 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304172 link_rate = event_data->PHY[i].LinkRate >> 4;
Eric Moore635374e2009-03-09 01:21:12 -06004173 switch (reason_code) {
4174 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4175 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304176
4177 mpt2sas_transport_update_links(ioc, sas_address,
4178 handle, phy_number, link_rate);
4179
4180 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4181 break;
Eric Moore635374e2009-03-09 01:21:12 -06004182 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
Eric Moore635374e2009-03-09 01:21:12 -06004183 _scsih_add_device(ioc, handle, phy_number, 0);
4184 }
4185 break;
4186 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304187
4188 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4189 sas_device = _scsih_sas_device_find_by_handle(ioc,
4190 handle);
4191 if (!sas_device) {
4192 spin_unlock_irqrestore(&ioc->sas_device_lock,
4193 flags);
4194 break;
4195 }
4196 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4197 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004198 break;
4199 }
4200 }
4201
4202 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304203 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4204 sas_expander)
4205 _scsih_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004206
4207}
4208
4209#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4210/**
4211 * _scsih_sas_device_status_change_event_debug - debug for device event
4212 * @event_data: event data payload
4213 * Context: user.
4214 *
4215 * Return nothing.
4216 */
4217static void
4218_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4219 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4220{
4221 char *reason_str = NULL;
4222
4223 switch (event_data->ReasonCode) {
4224 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4225 reason_str = "smart data";
4226 break;
4227 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4228 reason_str = "unsupported device discovered";
4229 break;
4230 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4231 reason_str = "internal device reset";
4232 break;
4233 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4234 reason_str = "internal task abort";
4235 break;
4236 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4237 reason_str = "internal task abort set";
4238 break;
4239 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4240 reason_str = "internal clear task set";
4241 break;
4242 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4243 reason_str = "internal query task";
4244 break;
4245 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4246 reason_str = "sata init failure";
4247 break;
4248 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4249 reason_str = "internal device reset complete";
4250 break;
4251 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4252 reason_str = "internal task abort complete";
4253 break;
4254 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4255 reason_str = "internal async notification";
4256 break;
4257 default:
4258 reason_str = "unknown reason";
4259 break;
4260 }
4261 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
4262 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4263 reason_str, le16_to_cpu(event_data->DevHandle),
4264 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4265 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4266 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4267 event_data->ASC, event_data->ASCQ);
4268 printk(KERN_INFO "\n");
4269}
4270#endif
4271
4272/**
4273 * _scsih_sas_device_status_change_event - handle device status change
4274 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304275 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004276 * Context: user.
4277 *
4278 * Return nothing.
4279 */
4280static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304281_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4282 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004283{
4284#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4285 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304286 _scsih_sas_device_status_change_event_debug(ioc,
4287 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004288#endif
4289}
4290
4291#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4292/**
4293 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4294 * @ioc: per adapter object
4295 * @event_data: event data payload
4296 * Context: user.
4297 *
4298 * Return nothing.
4299 */
4300static void
4301_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4302 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4303{
4304 char *reason_str = NULL;
4305
4306 switch (event_data->ReasonCode) {
4307 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4308 reason_str = "enclosure add";
4309 break;
4310 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4311 reason_str = "enclosure remove";
4312 break;
4313 default:
4314 reason_str = "unknown reason";
4315 break;
4316 }
4317
4318 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4319 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4320 " number slots(%d)\n", ioc->name, reason_str,
4321 le16_to_cpu(event_data->EnclosureHandle),
4322 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4323 le16_to_cpu(event_data->StartSlot));
4324}
4325#endif
4326
4327/**
4328 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4329 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304330 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004331 * Context: user.
4332 *
4333 * Return nothing.
4334 */
4335static void
4336_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304337 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004338{
4339#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4340 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4341 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304342 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004343#endif
4344}
4345
4346/**
4347 * _scsih_sas_broadcast_primative_event - handle broadcast events
4348 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304349 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004350 * Context: user.
4351 *
4352 * Return nothing.
4353 */
4354static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304355_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4356 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004357{
4358 struct scsi_cmnd *scmd;
4359 u16 smid, handle;
4360 u32 lun;
4361 struct MPT2SAS_DEVICE *sas_device_priv_data;
4362 u32 termination_count;
4363 u32 query_count;
4364 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304365#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4366 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4367#endif
Eric Moore635374e2009-03-09 01:21:12 -06004368 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4369 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4370 event_data->PortWidth));
Eric Moore635374e2009-03-09 01:21:12 -06004371 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4372 __func__));
4373
4374 mutex_lock(&ioc->tm_cmds.mutex);
4375 termination_count = 0;
4376 query_count = 0;
4377 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304378 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06004379 scmd = _scsih_scsi_lookup_get(ioc, smid);
4380 if (!scmd)
4381 continue;
4382 sas_device_priv_data = scmd->device->hostdata;
4383 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4384 continue;
4385 /* skip hidden raid components */
4386 if (sas_device_priv_data->sas_target->flags &
4387 MPT_TARGET_FLAGS_RAID_COMPONENT)
4388 continue;
4389 /* skip volumes */
4390 if (sas_device_priv_data->sas_target->flags &
4391 MPT_TARGET_FLAGS_VOLUME)
4392 continue;
4393
4394 handle = sas_device_priv_data->sas_target->handle;
4395 lun = sas_device_priv_data->lun;
4396 query_count++;
4397
4398 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4399 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
Eric Moore8901cbb2009-04-21 15:41:32 -06004400 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004401
4402 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4403 (mpi_reply->ResponseCode ==
4404 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4405 mpi_reply->ResponseCode ==
4406 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4407 continue;
4408
4409 mpt2sas_scsih_issue_tm(ioc, handle, lun,
Eric Moore8901cbb2009-04-21 15:41:32 -06004410 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4411 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004412 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4413 }
Eric Moore635374e2009-03-09 01:21:12 -06004414 ioc->broadcast_aen_busy = 0;
4415 mutex_unlock(&ioc->tm_cmds.mutex);
4416
4417 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4418 "%s - exit, query_count = %d termination_count = %d\n",
4419 ioc->name, __func__, query_count, termination_count));
4420}
4421
4422/**
4423 * _scsih_sas_discovery_event - handle discovery events
4424 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304425 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004426 * Context: user.
4427 *
4428 * Return nothing.
4429 */
4430static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304431_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
4432 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004433{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304434 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
4435
Eric Moore635374e2009-03-09 01:21:12 -06004436#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4437 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4438 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4439 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4440 "start" : "stop");
4441 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304442 printk("discovery_status(0x%08x)",
4443 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06004444 printk("\n");
4445 }
4446#endif
4447
4448 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4449 !ioc->sas_hba.num_phys)
4450 _scsih_sas_host_add(ioc);
4451}
4452
4453/**
4454 * _scsih_reprobe_lun - reprobing lun
4455 * @sdev: scsi device struct
4456 * @no_uld_attach: sdev->no_uld_attach flag setting
4457 *
4458 **/
4459static void
4460_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4461{
4462 int rc;
4463
4464 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4465 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4466 sdev->no_uld_attach ? "hidding" : "exposing");
4467 rc = scsi_device_reprobe(sdev);
4468}
4469
4470/**
4471 * _scsih_reprobe_target - reprobing target
4472 * @starget: scsi target struct
4473 * @no_uld_attach: sdev->no_uld_attach flag setting
4474 *
4475 * Note: no_uld_attach flag determines whether the disk device is attached
4476 * to block layer. A value of `1` means to not attach.
4477 **/
4478static void
4479_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4480{
4481 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4482
4483 if (no_uld_attach)
4484 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4485 else
4486 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4487
4488 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4489 _scsih_reprobe_lun);
4490}
4491/**
4492 * _scsih_sas_volume_add - add new volume
4493 * @ioc: per adapter object
4494 * @element: IR config element data
4495 * Context: user.
4496 *
4497 * Return nothing.
4498 */
4499static void
4500_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4501 Mpi2EventIrConfigElement_t *element)
4502{
4503 struct _raid_device *raid_device;
4504 unsigned long flags;
4505 u64 wwid;
4506 u16 handle = le16_to_cpu(element->VolDevHandle);
4507 int rc;
4508
Eric Moore635374e2009-03-09 01:21:12 -06004509 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4510 if (!wwid) {
4511 printk(MPT2SAS_ERR_FMT
4512 "failure at %s:%d/%s()!\n", ioc->name,
4513 __FILE__, __LINE__, __func__);
4514 return;
4515 }
4516
4517 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4518 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4519 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4520
4521 if (raid_device)
4522 return;
4523
4524 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4525 if (!raid_device) {
4526 printk(MPT2SAS_ERR_FMT
4527 "failure at %s:%d/%s()!\n", ioc->name,
4528 __FILE__, __LINE__, __func__);
4529 return;
4530 }
4531
4532 raid_device->id = ioc->sas_id++;
4533 raid_device->channel = RAID_CHANNEL;
4534 raid_device->handle = handle;
4535 raid_device->wwid = wwid;
4536 _scsih_raid_device_add(ioc, raid_device);
4537 if (!ioc->wait_for_port_enable_to_complete) {
4538 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4539 raid_device->id, 0);
4540 if (rc)
4541 _scsih_raid_device_remove(ioc, raid_device);
4542 } else
4543 _scsih_determine_boot_device(ioc, raid_device, 1);
4544}
4545
4546/**
4547 * _scsih_sas_volume_delete - delete volume
4548 * @ioc: per adapter object
4549 * @element: IR config element data
4550 * Context: user.
4551 *
4552 * Return nothing.
4553 */
4554static void
4555_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4556 Mpi2EventIrConfigElement_t *element)
4557{
4558 struct _raid_device *raid_device;
4559 u16 handle = le16_to_cpu(element->VolDevHandle);
4560 unsigned long flags;
4561 struct MPT2SAS_TARGET *sas_target_priv_data;
4562
Eric Moore635374e2009-03-09 01:21:12 -06004563 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4564 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4565 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4566 if (!raid_device)
4567 return;
4568 if (raid_device->starget) {
4569 sas_target_priv_data = raid_device->starget->hostdata;
4570 sas_target_priv_data->deleted = 1;
4571 scsi_remove_target(&raid_device->starget->dev);
4572 }
4573 _scsih_raid_device_remove(ioc, raid_device);
4574}
4575
4576/**
4577 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4578 * @ioc: per adapter object
4579 * @element: IR config element data
4580 * Context: user.
4581 *
4582 * Return nothing.
4583 */
4584static void
4585_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4586 Mpi2EventIrConfigElement_t *element)
4587{
4588 struct _sas_device *sas_device;
4589 unsigned long flags;
4590 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4591
4592 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4593 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4594 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4595 if (!sas_device)
4596 return;
4597
4598 /* exposing raid component */
4599 sas_device->volume_handle = 0;
4600 sas_device->volume_wwid = 0;
4601 sas_device->hidden_raid_component = 0;
4602 _scsih_reprobe_target(sas_device->starget, 0);
4603}
4604
4605/**
4606 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4607 * @ioc: per adapter object
4608 * @element: IR config element data
4609 * Context: user.
4610 *
4611 * Return nothing.
4612 */
4613static void
4614_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4615 Mpi2EventIrConfigElement_t *element)
4616{
4617 struct _sas_device *sas_device;
4618 unsigned long flags;
4619 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4620
4621 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4622 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4623 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4624 if (!sas_device)
4625 return;
4626
4627 /* hiding raid component */
4628 mpt2sas_config_get_volume_handle(ioc, handle,
4629 &sas_device->volume_handle);
4630 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4631 &sas_device->volume_wwid);
4632 sas_device->hidden_raid_component = 1;
4633 _scsih_reprobe_target(sas_device->starget, 1);
4634}
4635
4636/**
4637 * _scsih_sas_pd_delete - delete pd component
4638 * @ioc: per adapter object
4639 * @element: IR config element data
4640 * Context: user.
4641 *
4642 * Return nothing.
4643 */
4644static void
4645_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4646 Mpi2EventIrConfigElement_t *element)
4647{
4648 struct _sas_device *sas_device;
4649 unsigned long flags;
4650 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4651
4652 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4653 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4654 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4655 if (!sas_device)
4656 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304657 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004658}
4659
4660/**
4661 * _scsih_sas_pd_add - remove pd component
4662 * @ioc: per adapter object
4663 * @element: IR config element data
4664 * Context: user.
4665 *
4666 * Return nothing.
4667 */
4668static void
4669_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4670 Mpi2EventIrConfigElement_t *element)
4671{
4672 struct _sas_device *sas_device;
4673 unsigned long flags;
4674 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304675 Mpi2ConfigReply_t mpi_reply;
4676 Mpi2SasDevicePage0_t sas_device_pg0;
4677 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304678 u64 sas_address;
4679 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06004680
4681 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4682 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4683 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304684 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004685 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304686 return;
4687 }
4688
4689 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4690 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4691 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4692 ioc->name, __FILE__, __LINE__, __func__);
4693 return;
4694 }
4695
4696 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4697 MPI2_IOCSTATUS_MASK;
4698 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4699 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4700 ioc->name, __FILE__, __LINE__, __func__);
4701 return;
4702 }
4703
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304704 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
4705 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
4706 mpt2sas_transport_update_links(ioc, sas_address, handle,
4707 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304708
4709 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06004710}
4711
4712#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4713/**
4714 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4715 * @ioc: per adapter object
4716 * @event_data: event data payload
4717 * Context: user.
4718 *
4719 * Return nothing.
4720 */
4721static void
4722_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4723 Mpi2EventDataIrConfigChangeList_t *event_data)
4724{
4725 Mpi2EventIrConfigElement_t *element;
4726 u8 element_type;
4727 int i;
4728 char *reason_str = NULL, *element_str = NULL;
4729
4730 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4731
4732 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4733 ioc->name, (le32_to_cpu(event_data->Flags) &
4734 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4735 "foreign" : "native", event_data->NumElements);
4736 for (i = 0; i < event_data->NumElements; i++, element++) {
4737 switch (element->ReasonCode) {
4738 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4739 reason_str = "add";
4740 break;
4741 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4742 reason_str = "remove";
4743 break;
4744 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4745 reason_str = "no change";
4746 break;
4747 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4748 reason_str = "hide";
4749 break;
4750 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4751 reason_str = "unhide";
4752 break;
4753 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4754 reason_str = "volume_created";
4755 break;
4756 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4757 reason_str = "volume_deleted";
4758 break;
4759 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4760 reason_str = "pd_created";
4761 break;
4762 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4763 reason_str = "pd_deleted";
4764 break;
4765 default:
4766 reason_str = "unknown reason";
4767 break;
4768 }
4769 element_type = le16_to_cpu(element->ElementFlags) &
4770 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4771 switch (element_type) {
4772 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4773 element_str = "volume";
4774 break;
4775 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4776 element_str = "phys disk";
4777 break;
4778 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4779 element_str = "hot spare";
4780 break;
4781 default:
4782 element_str = "unknown element";
4783 break;
4784 }
4785 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4786 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4787 reason_str, le16_to_cpu(element->VolDevHandle),
4788 le16_to_cpu(element->PhysDiskDevHandle),
4789 element->PhysDiskNum);
4790 }
4791}
4792#endif
4793
4794/**
4795 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4796 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304797 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004798 * Context: user.
4799 *
4800 * Return nothing.
4801 */
4802static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304803_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
4804 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004805{
4806 Mpi2EventIrConfigElement_t *element;
4807 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304808 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304809 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004810
4811#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4812 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4813 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4814
4815#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05304816 foreign_config = (le32_to_cpu(event_data->Flags) &
4817 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06004818
4819 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4820 for (i = 0; i < event_data->NumElements; i++, element++) {
4821
4822 switch (element->ReasonCode) {
4823 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4824 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304825 if (!foreign_config)
4826 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06004827 break;
4828 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4829 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304830 if (!foreign_config)
4831 _scsih_sas_volume_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06004832 break;
4833 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4834 _scsih_sas_pd_hide(ioc, element);
4835 break;
4836 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4837 _scsih_sas_pd_expose(ioc, element);
4838 break;
4839 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4840 _scsih_sas_pd_add(ioc, element);
4841 break;
4842 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4843 _scsih_sas_pd_delete(ioc, element);
4844 break;
4845 }
4846 }
4847}
4848
4849/**
4850 * _scsih_sas_ir_volume_event - IR volume event
4851 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304852 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004853 * Context: user.
4854 *
4855 * Return nothing.
4856 */
4857static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304858_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
4859 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004860{
4861 u64 wwid;
4862 unsigned long flags;
4863 struct _raid_device *raid_device;
4864 u16 handle;
4865 u32 state;
4866 int rc;
4867 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304868 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004869
4870 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4871 return;
4872
4873 handle = le16_to_cpu(event_data->VolDevHandle);
4874 state = le32_to_cpu(event_data->NewValue);
4875 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4876 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4877 le32_to_cpu(event_data->PreviousValue), state));
4878
4879 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4880 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4881 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4882
4883 switch (state) {
4884 case MPI2_RAID_VOL_STATE_MISSING:
4885 case MPI2_RAID_VOL_STATE_FAILED:
4886 if (!raid_device)
4887 break;
4888 if (raid_device->starget) {
4889 sas_target_priv_data = raid_device->starget->hostdata;
4890 sas_target_priv_data->deleted = 1;
4891 scsi_remove_target(&raid_device->starget->dev);
4892 }
4893 _scsih_raid_device_remove(ioc, raid_device);
4894 break;
4895
4896 case MPI2_RAID_VOL_STATE_ONLINE:
4897 case MPI2_RAID_VOL_STATE_DEGRADED:
4898 case MPI2_RAID_VOL_STATE_OPTIMAL:
4899 if (raid_device)
4900 break;
4901
4902 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4903 if (!wwid) {
4904 printk(MPT2SAS_ERR_FMT
4905 "failure at %s:%d/%s()!\n", ioc->name,
4906 __FILE__, __LINE__, __func__);
4907 break;
4908 }
4909
4910 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4911 if (!raid_device) {
4912 printk(MPT2SAS_ERR_FMT
4913 "failure at %s:%d/%s()!\n", ioc->name,
4914 __FILE__, __LINE__, __func__);
4915 break;
4916 }
4917
4918 raid_device->id = ioc->sas_id++;
4919 raid_device->channel = RAID_CHANNEL;
4920 raid_device->handle = handle;
4921 raid_device->wwid = wwid;
4922 _scsih_raid_device_add(ioc, raid_device);
4923 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4924 raid_device->id, 0);
4925 if (rc)
4926 _scsih_raid_device_remove(ioc, raid_device);
4927 break;
4928
4929 case MPI2_RAID_VOL_STATE_INITIALIZING:
4930 default:
4931 break;
4932 }
4933}
4934
4935/**
4936 * _scsih_sas_ir_physical_disk_event - PD event
4937 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304938 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004939 * Context: user.
4940 *
4941 * Return nothing.
4942 */
4943static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304944_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
4945 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004946{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304947 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06004948 u32 state;
4949 struct _sas_device *sas_device;
4950 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304951 Mpi2ConfigReply_t mpi_reply;
4952 Mpi2SasDevicePage0_t sas_device_pg0;
4953 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304954 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304955 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004956
4957 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4958 return;
4959
4960 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4961 state = le32_to_cpu(event_data->NewValue);
4962
4963 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4964 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4965 le32_to_cpu(event_data->PreviousValue), state));
4966
4967 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4968 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4969 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4970
4971 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06004972 case MPI2_RAID_PD_STATE_ONLINE:
4973 case MPI2_RAID_PD_STATE_DEGRADED:
4974 case MPI2_RAID_PD_STATE_REBUILDING:
4975 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304976 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004977 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304978 return;
4979 }
4980
4981 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4982 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
4983 handle))) {
4984 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4985 ioc->name, __FILE__, __LINE__, __func__);
4986 return;
4987 }
4988
4989 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4990 MPI2_IOCSTATUS_MASK;
4991 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4992 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4993 ioc->name, __FILE__, __LINE__, __func__);
4994 return;
4995 }
4996
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304997 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
4998 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
4999 mpt2sas_transport_update_links(ioc, sas_address, handle,
5000 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305001
5002 _scsih_add_device(ioc, handle, 0, 1);
5003
Eric Moore635374e2009-03-09 01:21:12 -06005004 break;
5005
Kashyap, Desai62727a72009-08-07 19:35:18 +05305006 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005007 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5008 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5009 case MPI2_RAID_PD_STATE_HOT_SPARE:
5010 default:
5011 break;
5012 }
5013}
5014
5015#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5016/**
5017 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5018 * @ioc: per adapter object
5019 * @event_data: event data payload
5020 * Context: user.
5021 *
5022 * Return nothing.
5023 */
5024static void
5025_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5026 Mpi2EventDataIrOperationStatus_t *event_data)
5027{
5028 char *reason_str = NULL;
5029
5030 switch (event_data->RAIDOperation) {
5031 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5032 reason_str = "resync";
5033 break;
5034 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5035 reason_str = "online capacity expansion";
5036 break;
5037 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5038 reason_str = "consistency check";
5039 break;
5040 default:
5041 reason_str = "unknown reason";
5042 break;
5043 }
5044
5045 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5046 "\thandle(0x%04x), percent complete(%d)\n",
5047 ioc->name, reason_str,
5048 le16_to_cpu(event_data->VolDevHandle),
5049 event_data->PercentComplete);
5050}
5051#endif
5052
5053/**
5054 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5055 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305056 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005057 * Context: user.
5058 *
5059 * Return nothing.
5060 */
5061static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305062_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5063 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005064{
5065#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5066 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305067 _scsih_sas_ir_operation_status_event_debug(ioc,
5068 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005069#endif
5070}
5071
5072/**
5073 * _scsih_task_set_full - handle task set full
5074 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305075 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005076 * Context: user.
5077 *
5078 * Throttle back qdepth.
5079 */
5080static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305081_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5082 *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005083{
5084 unsigned long flags;
5085 struct _sas_device *sas_device;
5086 static struct _raid_device *raid_device;
5087 struct scsi_device *sdev;
5088 int depth;
5089 u16 current_depth;
5090 u16 handle;
5091 int id, channel;
5092 u64 sas_address;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305093 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005094
5095 current_depth = le16_to_cpu(event_data->CurrentDepth);
5096 handle = le16_to_cpu(event_data->DevHandle);
5097 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5098 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5099 if (!sas_device) {
5100 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5101 return;
5102 }
5103 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5104 id = sas_device->id;
5105 channel = sas_device->channel;
5106 sas_address = sas_device->sas_address;
5107
5108 /* if hidden raid component, then change to volume characteristics */
5109 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
5110 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5111 raid_device = _scsih_raid_device_find_by_handle(
5112 ioc, sas_device->volume_handle);
5113 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5114 if (raid_device) {
5115 id = raid_device->id;
5116 channel = raid_device->channel;
5117 handle = raid_device->handle;
5118 sas_address = raid_device->wwid;
5119 }
5120 }
5121
5122 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5123 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
5124 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5125 handle, (unsigned long long)sas_address, current_depth);
5126
5127 shost_for_each_device(sdev, ioc->shost) {
5128 if (sdev->id == id && sdev->channel == channel) {
5129 if (current_depth > sdev->queue_depth) {
5130 if (ioc->logging_level &
5131 MPT_DEBUG_TASK_SET_FULL)
5132 sdev_printk(KERN_INFO, sdev, "strange "
5133 "observation, the queue depth is"
5134 " (%d) meanwhile fw queue depth "
5135 "is (%d)\n", sdev->queue_depth,
5136 current_depth);
5137 continue;
5138 }
5139 depth = scsi_track_queue_full(sdev,
5140 current_depth - 1);
5141 if (depth > 0)
5142 sdev_printk(KERN_INFO, sdev, "Queue depth "
5143 "reduced to (%d)\n", depth);
5144 else if (depth < 0)
5145 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5146 "Queueing is being disabled\n");
5147 else if (depth == 0)
5148 if (ioc->logging_level &
5149 MPT_DEBUG_TASK_SET_FULL)
5150 sdev_printk(KERN_INFO, sdev,
5151 "Queue depth not changed yet\n");
5152 }
5153 }
5154}
5155
5156/**
5157 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5158 * @ioc: per adapter object
5159 * @sas_address: sas address
5160 * @slot: enclosure slot id
5161 * @handle: device handle
5162 *
5163 * After host reset, find out whether devices are still responding.
5164 * Used in _scsi_remove_unresponsive_sas_devices.
5165 *
5166 * Return nothing.
5167 */
5168static void
5169_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5170 u16 slot, u16 handle)
5171{
5172 struct MPT2SAS_TARGET *sas_target_priv_data;
5173 struct scsi_target *starget;
5174 struct _sas_device *sas_device;
5175 unsigned long flags;
5176
5177 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5178 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5179 if (sas_device->sas_address == sas_address &&
5180 sas_device->slot == slot && sas_device->starget) {
5181 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305182 sas_device->state = 0;
5183 starget = sas_device->starget;
5184 sas_target_priv_data = starget->hostdata;
5185 sas_target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005186 starget_printk(KERN_INFO, sas_device->starget,
5187 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5188 "logical id(0x%016llx), slot(%d)\n", handle,
5189 (unsigned long long)sas_device->sas_address,
5190 (unsigned long long)
5191 sas_device->enclosure_logical_id,
5192 sas_device->slot);
5193 if (sas_device->handle == handle)
5194 goto out;
5195 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5196 sas_device->handle);
5197 sas_device->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005198 sas_target_priv_data->handle = handle;
5199 goto out;
5200 }
5201 }
5202 out:
5203 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5204}
5205
5206/**
5207 * _scsih_search_responding_sas_devices -
5208 * @ioc: per adapter object
5209 *
5210 * After host reset, find out whether devices are still responding.
5211 * If not remove.
5212 *
5213 * Return nothing.
5214 */
5215static void
5216_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5217{
5218 Mpi2SasDevicePage0_t sas_device_pg0;
5219 Mpi2ConfigReply_t mpi_reply;
5220 u16 ioc_status;
5221 __le64 sas_address;
5222 u16 handle;
5223 u32 device_info;
5224 u16 slot;
5225
5226 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5227
5228 if (list_empty(&ioc->sas_device_list))
5229 return;
5230
5231 handle = 0xFFFF;
5232 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5233 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5234 handle))) {
5235 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5236 MPI2_IOCSTATUS_MASK;
5237 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5238 break;
5239 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5240 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5241 if (!(_scsih_is_end_device(device_info)))
5242 continue;
5243 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5244 slot = le16_to_cpu(sas_device_pg0.Slot);
5245 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5246 handle);
5247 }
5248}
5249
5250/**
5251 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5252 * @ioc: per adapter object
5253 * @wwid: world wide identifier for raid volume
5254 * @handle: device handle
5255 *
5256 * After host reset, find out whether devices are still responding.
5257 * Used in _scsi_remove_unresponsive_raid_devices.
5258 *
5259 * Return nothing.
5260 */
5261static void
5262_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5263 u16 handle)
5264{
5265 struct MPT2SAS_TARGET *sas_target_priv_data;
5266 struct scsi_target *starget;
5267 struct _raid_device *raid_device;
5268 unsigned long flags;
5269
5270 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5271 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5272 if (raid_device->wwid == wwid && raid_device->starget) {
5273 raid_device->responding = 1;
5274 starget_printk(KERN_INFO, raid_device->starget,
5275 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5276 (unsigned long long)raid_device->wwid);
5277 if (raid_device->handle == handle)
5278 goto out;
5279 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5280 raid_device->handle);
5281 raid_device->handle = handle;
5282 starget = raid_device->starget;
5283 sas_target_priv_data = starget->hostdata;
5284 sas_target_priv_data->handle = handle;
5285 goto out;
5286 }
5287 }
5288 out:
5289 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5290}
5291
5292/**
5293 * _scsih_search_responding_raid_devices -
5294 * @ioc: per adapter object
5295 *
5296 * After host reset, find out whether devices are still responding.
5297 * If not remove.
5298 *
5299 * Return nothing.
5300 */
5301static void
5302_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5303{
5304 Mpi2RaidVolPage1_t volume_pg1;
5305 Mpi2ConfigReply_t mpi_reply;
5306 u16 ioc_status;
5307 u16 handle;
5308
5309 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5310
5311 if (list_empty(&ioc->raid_device_list))
5312 return;
5313
5314 handle = 0xFFFF;
5315 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5316 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5317 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5318 MPI2_IOCSTATUS_MASK;
5319 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5320 break;
5321 handle = le16_to_cpu(volume_pg1.DevHandle);
5322 _scsih_mark_responding_raid_device(ioc,
5323 le64_to_cpu(volume_pg1.WWID), handle);
5324 }
5325}
5326
5327/**
5328 * _scsih_mark_responding_expander - mark a expander as responding
5329 * @ioc: per adapter object
5330 * @sas_address: sas address
5331 * @handle:
5332 *
5333 * After host reset, find out whether devices are still responding.
5334 * Used in _scsi_remove_unresponsive_expanders.
5335 *
5336 * Return nothing.
5337 */
5338static void
5339_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5340 u16 handle)
5341{
5342 struct _sas_node *sas_expander;
5343 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305344 int i;
Eric Moore635374e2009-03-09 01:21:12 -06005345
5346 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5347 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305348 if (sas_expander->sas_address != sas_address)
5349 continue;
5350 sas_expander->responding = 1;
5351 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06005352 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305353 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
5354 " from(0x%04x) to (0x%04x)!!!\n",
5355 (unsigned long long)sas_expander->sas_address,
5356 sas_expander->handle, handle);
5357 sas_expander->handle = handle;
5358 for (i = 0 ; i < sas_expander->num_phys ; i++)
5359 sas_expander->phy[i].handle = handle;
5360 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005361 }
5362 out:
5363 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5364}
5365
5366/**
5367 * _scsih_search_responding_expanders -
5368 * @ioc: per adapter object
5369 *
5370 * After host reset, find out whether devices are still responding.
5371 * If not remove.
5372 *
5373 * Return nothing.
5374 */
5375static void
5376_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5377{
5378 Mpi2ExpanderPage0_t expander_pg0;
5379 Mpi2ConfigReply_t mpi_reply;
5380 u16 ioc_status;
5381 __le64 sas_address;
5382 u16 handle;
5383
5384 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5385
5386 if (list_empty(&ioc->sas_expander_list))
5387 return;
5388
5389 handle = 0xFFFF;
5390 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5391 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5392
5393 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5394 MPI2_IOCSTATUS_MASK;
5395 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5396 break;
5397
5398 handle = le16_to_cpu(expander_pg0.DevHandle);
5399 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5400 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5401 "sas_addr(0x%016llx)\n", handle,
5402 (unsigned long long)sas_address);
5403 _scsih_mark_responding_expander(ioc, sas_address, handle);
5404 }
5405
5406}
5407
5408/**
5409 * _scsih_remove_unresponding_devices - removing unresponding devices
5410 * @ioc: per adapter object
5411 *
5412 * Return nothing.
5413 */
5414static void
5415_scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5416{
5417 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305418 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06005419 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06005420
Eric Moore635374e2009-03-09 01:21:12 -06005421
5422 list_for_each_entry_safe(sas_device, sas_device_next,
5423 &ioc->sas_device_list, list) {
5424 if (sas_device->responding) {
5425 sas_device->responding = 0;
5426 continue;
5427 }
5428 if (sas_device->starget)
5429 starget_printk(KERN_INFO, sas_device->starget,
5430 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5431 "enclosure logical id(0x%016llx), slot(%d)\n",
5432 sas_device->handle,
5433 (unsigned long long)sas_device->sas_address,
5434 (unsigned long long)
5435 sas_device->enclosure_logical_id,
5436 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305437 /* invalidate the device handle */
5438 sas_device->handle = 0;
5439 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005440 }
5441
5442 list_for_each_entry_safe(raid_device, raid_device_next,
5443 &ioc->raid_device_list, list) {
5444 if (raid_device->responding) {
5445 raid_device->responding = 0;
5446 continue;
5447 }
5448 if (raid_device->starget) {
5449 starget_printk(KERN_INFO, raid_device->starget,
5450 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5451 raid_device->handle,
5452 (unsigned long long)raid_device->wwid);
5453 scsi_remove_target(&raid_device->starget->dev);
5454 }
5455 _scsih_raid_device_remove(ioc, raid_device);
5456 }
5457
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305458 retry_expander_search:
5459 sas_expander = NULL;
5460 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06005461 if (sas_expander->responding) {
5462 sas_expander->responding = 0;
5463 continue;
5464 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305465 _scsih_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305466 goto retry_expander_search;
5467 }
5468}
5469
5470/**
5471 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5472 * @ioc: per adapter object
5473 * @reset_phase: phase
5474 *
5475 * The handler for doing any required cleanup or initialization.
5476 *
5477 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5478 * MPT2_IOC_DONE_RESET
5479 *
5480 * Return nothing.
5481 */
5482void
5483mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5484{
5485 switch (reset_phase) {
5486 case MPT2_IOC_PRE_RESET:
5487 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5488 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5489 _scsih_fw_event_off(ioc);
5490 break;
5491 case MPT2_IOC_AFTER_RESET:
5492 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5493 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5494 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5495 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5496 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5497 complete(&ioc->tm_cmds.done);
5498 }
5499 _scsih_fw_event_on(ioc);
5500 _scsih_flush_running_cmds(ioc);
5501 break;
5502 case MPT2_IOC_DONE_RESET:
5503 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5504 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305505 _scsih_sas_host_refresh(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305506 _scsih_search_responding_sas_devices(ioc);
5507 _scsih_search_responding_raid_devices(ioc);
5508 _scsih_search_responding_expanders(ioc);
5509 break;
5510 case MPT2_IOC_RUNNING:
5511 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5512 "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5513 _scsih_remove_unresponding_devices(ioc);
5514 break;
Eric Moore635374e2009-03-09 01:21:12 -06005515 }
5516}
5517
5518/**
5519 * _firmware_event_work - delayed task for processing firmware events
5520 * @ioc: per adapter object
5521 * @work: equal to the fw_event_work object
5522 * Context: user.
5523 *
5524 * Return nothing.
5525 */
5526static void
5527_firmware_event_work(struct work_struct *work)
5528{
5529 struct fw_event_work *fw_event = container_of(work,
Eric Moore6f92a7a2009-04-21 15:43:33 -06005530 struct fw_event_work, work);
Eric Moore635374e2009-03-09 01:21:12 -06005531 unsigned long flags;
5532 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5533
Eric Moore635374e2009-03-09 01:21:12 -06005534 /* the queue is being flushed so ignore this event */
5535 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5536 if (ioc->fw_events_off || ioc->remove_host) {
5537 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5538 _scsih_fw_event_free(ioc, fw_event);
5539 return;
5540 }
5541 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5542
Eric Moore635374e2009-03-09 01:21:12 -06005543 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06005544 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5545 return;
5546 }
Eric Moore635374e2009-03-09 01:21:12 -06005547
5548 switch (fw_event->event) {
5549 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305550 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005551 break;
5552 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305553 _scsih_sas_device_status_change_event(ioc,
5554 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005555 break;
5556 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305557 _scsih_sas_discovery_event(ioc,
5558 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005559 break;
5560 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305561 _scsih_sas_broadcast_primative_event(ioc,
5562 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005563 break;
5564 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5565 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305566 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005567 break;
5568 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305569 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005570 break;
5571 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305572 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005573 break;
5574 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305575 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005576 break;
5577 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305578 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005579 break;
5580 case MPI2_EVENT_TASK_SET_FULL:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305581 _scsih_task_set_full(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005582 break;
5583 }
5584 _scsih_fw_event_free(ioc, fw_event);
5585}
5586
5587/**
5588 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5589 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305590 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06005591 * @reply: reply message frame(lower 32bit addr)
5592 * Context: interrupt.
5593 *
5594 * This function merely adds a new work task into ioc->firmware_event_thread.
5595 * The tasks are worked from _firmware_event_work in user context.
5596 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305597 * Return 1 meaning mf should be freed from _base_interrupt
5598 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06005599 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305600u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305601mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
5602 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06005603{
5604 struct fw_event_work *fw_event;
5605 Mpi2EventNotificationReply_t *mpi_reply;
5606 unsigned long flags;
5607 u16 event;
5608
5609 /* events turned off due to host reset or driver unloading */
5610 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5611 if (ioc->fw_events_off || ioc->remove_host) {
5612 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305613 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005614 }
5615 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5616
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305617 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06005618 event = le16_to_cpu(mpi_reply->Event);
5619
5620 switch (event) {
5621 /* handle these */
5622 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5623 {
5624 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5625 (Mpi2EventDataSasBroadcastPrimitive_t *)
5626 mpi_reply->EventData;
5627
5628 if (baen_data->Primitive !=
5629 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5630 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305631 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005632 ioc->broadcast_aen_busy = 1;
5633 break;
5634 }
5635
5636 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5637 _scsih_check_topo_delete_events(ioc,
5638 (Mpi2EventDataSasTopologyChangeList_t *)
5639 mpi_reply->EventData);
5640 break;
5641
5642 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5643 case MPI2_EVENT_IR_OPERATION_STATUS:
5644 case MPI2_EVENT_SAS_DISCOVERY:
5645 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5646 case MPI2_EVENT_IR_VOLUME:
5647 case MPI2_EVENT_IR_PHYSICAL_DISK:
5648 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5649 case MPI2_EVENT_TASK_SET_FULL:
5650 break;
5651
5652 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305653 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005654 }
5655
5656 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5657 if (!fw_event) {
5658 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5659 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305660 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005661 }
5662 fw_event->event_data =
5663 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5664 if (!fw_event->event_data) {
5665 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5666 ioc->name, __FILE__, __LINE__, __func__);
5667 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305668 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005669 }
5670
5671 memcpy(fw_event->event_data, mpi_reply->EventData,
5672 mpi_reply->EventDataLength*4);
5673 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305674 fw_event->VF_ID = mpi_reply->VF_ID;
5675 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06005676 fw_event->event = event;
5677 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305678 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005679}
5680
5681/* shost template */
5682static struct scsi_host_template scsih_driver_template = {
5683 .module = THIS_MODULE,
5684 .name = "Fusion MPT SAS Host",
5685 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06005686 .queuecommand = _scsih_qcmd,
5687 .target_alloc = _scsih_target_alloc,
5688 .slave_alloc = _scsih_slave_alloc,
5689 .slave_configure = _scsih_slave_configure,
5690 .target_destroy = _scsih_target_destroy,
5691 .slave_destroy = _scsih_slave_destroy,
5692 .change_queue_depth = _scsih_change_queue_depth,
5693 .change_queue_type = _scsih_change_queue_type,
5694 .eh_abort_handler = _scsih_abort,
5695 .eh_device_reset_handler = _scsih_dev_reset,
5696 .eh_target_reset_handler = _scsih_target_reset,
5697 .eh_host_reset_handler = _scsih_host_reset,
5698 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06005699 .can_queue = 1,
5700 .this_id = -1,
5701 .sg_tablesize = MPT2SAS_SG_DEPTH,
5702 .max_sectors = 8192,
5703 .cmd_per_lun = 7,
5704 .use_clustering = ENABLE_CLUSTERING,
5705 .shost_attrs = mpt2sas_host_attrs,
5706 .sdev_attrs = mpt2sas_dev_attrs,
5707};
5708
5709/**
5710 * _scsih_expander_node_remove - removing expander device from list.
5711 * @ioc: per adapter object
5712 * @sas_expander: the sas_device object
5713 * Context: Calling function should acquire ioc->sas_node_lock.
5714 *
5715 * Removing object and freeing associated memory from the
5716 * ioc->sas_expander_list.
5717 *
5718 * Return nothing.
5719 */
5720static void
5721_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5722 struct _sas_node *sas_expander)
5723{
5724 struct _sas_port *mpt2sas_port;
5725 struct _sas_device *sas_device;
5726 struct _sas_node *expander_sibling;
5727 unsigned long flags;
5728
5729 if (!sas_expander)
5730 return;
5731
5732 /* remove sibling ports attached to this expander */
5733 retry_device_search:
5734 list_for_each_entry(mpt2sas_port,
5735 &sas_expander->sas_port_list, port_list) {
5736 if (mpt2sas_port->remote_identify.device_type ==
5737 SAS_END_DEVICE) {
5738 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5739 sas_device =
5740 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5741 mpt2sas_port->remote_identify.sas_address);
5742 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5743 if (!sas_device)
5744 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305745 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305746 if (ioc->shost_recovery)
5747 return;
Eric Moore635374e2009-03-09 01:21:12 -06005748 goto retry_device_search;
5749 }
5750 }
5751
5752 retry_expander_search:
5753 list_for_each_entry(mpt2sas_port,
5754 &sas_expander->sas_port_list, port_list) {
5755
5756 if (mpt2sas_port->remote_identify.device_type ==
5757 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5758 mpt2sas_port->remote_identify.device_type ==
5759 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5760
5761 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5762 expander_sibling =
5763 mpt2sas_scsih_expander_find_by_sas_address(
5764 ioc, mpt2sas_port->remote_identify.sas_address);
5765 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5766 if (!expander_sibling)
5767 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305768 _scsih_expander_remove(ioc,
5769 expander_sibling->sas_address);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305770 if (ioc->shost_recovery)
5771 return;
Eric Moore635374e2009-03-09 01:21:12 -06005772 goto retry_expander_search;
5773 }
5774 }
5775
5776 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305777 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005778
5779 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5780 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5781 sas_expander->handle, (unsigned long long)
5782 sas_expander->sas_address);
5783
5784 list_del(&sas_expander->list);
5785 kfree(sas_expander->phy);
5786 kfree(sas_expander);
5787}
5788
5789/**
Eric Moored5d135b2009-05-18 13:02:08 -06005790 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06005791 * @pdev: PCI device struct
5792 *
5793 * Return nothing.
5794 */
5795static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06005796_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06005797{
5798 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5799 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5800 struct _sas_port *mpt2sas_port;
5801 struct _sas_device *sas_device;
5802 struct _sas_node *expander_sibling;
5803 struct workqueue_struct *wq;
5804 unsigned long flags;
5805
5806 ioc->remove_host = 1;
5807 _scsih_fw_event_off(ioc);
5808
5809 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5810 wq = ioc->firmware_event_thread;
5811 ioc->firmware_event_thread = NULL;
5812 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5813 if (wq)
5814 destroy_workqueue(wq);
5815
5816 /* free ports attached to the sas_host */
5817 retry_again:
5818 list_for_each_entry(mpt2sas_port,
5819 &ioc->sas_hba.sas_port_list, port_list) {
5820 if (mpt2sas_port->remote_identify.device_type ==
5821 SAS_END_DEVICE) {
5822 sas_device =
5823 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5824 mpt2sas_port->remote_identify.sas_address);
5825 if (sas_device) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305826 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005827 goto retry_again;
5828 }
5829 } else {
5830 expander_sibling =
5831 mpt2sas_scsih_expander_find_by_sas_address(ioc,
5832 mpt2sas_port->remote_identify.sas_address);
5833 if (expander_sibling) {
5834 _scsih_expander_remove(ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305835 expander_sibling->sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005836 goto retry_again;
5837 }
5838 }
5839 }
5840
5841 /* free phys attached to the sas_host */
5842 if (ioc->sas_hba.num_phys) {
5843 kfree(ioc->sas_hba.phy);
5844 ioc->sas_hba.phy = NULL;
5845 ioc->sas_hba.num_phys = 0;
5846 }
5847
5848 sas_remove_host(shost);
5849 mpt2sas_base_detach(ioc);
5850 list_del(&ioc->list);
5851 scsi_remove_host(shost);
5852 scsi_host_put(shost);
5853}
5854
5855/**
5856 * _scsih_probe_boot_devices - reports 1st device
5857 * @ioc: per adapter object
5858 *
5859 * If specified in bios page 2, this routine reports the 1st
5860 * device scsi-ml or sas transport for persistent boot device
5861 * purposes. Please refer to function _scsih_determine_boot_device()
5862 */
5863static void
5864_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5865{
5866 u8 is_raid;
5867 void *device;
5868 struct _sas_device *sas_device;
5869 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305870 u16 handle;
5871 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06005872 u64 sas_address;
5873 unsigned long flags;
5874 int rc;
5875
5876 device = NULL;
5877 if (ioc->req_boot_device.device) {
5878 device = ioc->req_boot_device.device;
5879 is_raid = ioc->req_boot_device.is_raid;
5880 } else if (ioc->req_alt_boot_device.device) {
5881 device = ioc->req_alt_boot_device.device;
5882 is_raid = ioc->req_alt_boot_device.is_raid;
5883 } else if (ioc->current_boot_device.device) {
5884 device = ioc->current_boot_device.device;
5885 is_raid = ioc->current_boot_device.is_raid;
5886 }
5887
5888 if (!device)
5889 return;
5890
5891 if (is_raid) {
5892 raid_device = device;
5893 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5894 raid_device->id, 0);
5895 if (rc)
5896 _scsih_raid_device_remove(ioc, raid_device);
5897 } else {
5898 sas_device = device;
5899 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305900 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06005901 sas_address = sas_device->sas_address;
5902 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5903 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5904 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5905 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305906 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06005907 _scsih_sas_device_remove(ioc, sas_device);
5908 } else if (!sas_device->starget) {
5909 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305910 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005911 _scsih_sas_device_remove(ioc, sas_device);
5912 }
5913 }
5914}
5915
5916/**
5917 * _scsih_probe_raid - reporting raid volumes to scsi-ml
5918 * @ioc: per adapter object
5919 *
5920 * Called during initial loading of the driver.
5921 */
5922static void
5923_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5924{
5925 struct _raid_device *raid_device, *raid_next;
5926 int rc;
5927
5928 list_for_each_entry_safe(raid_device, raid_next,
5929 &ioc->raid_device_list, list) {
5930 if (raid_device->starget)
5931 continue;
5932 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5933 raid_device->id, 0);
5934 if (rc)
5935 _scsih_raid_device_remove(ioc, raid_device);
5936 }
5937}
5938
5939/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305940 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06005941 * @ioc: per adapter object
5942 *
5943 * Called during initial loading of the driver.
5944 */
5945static void
5946_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5947{
5948 struct _sas_device *sas_device, *next;
5949 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06005950
5951 /* SAS Device List */
5952 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5953 list) {
5954 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5955 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5956 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5957
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305958 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5959 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06005960 _scsih_sas_device_remove(ioc, sas_device);
5961 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305962 mpt2sas_transport_port_remove(ioc,
5963 sas_device->sas_address,
5964 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005965 _scsih_sas_device_remove(ioc, sas_device);
5966 }
5967 }
5968}
5969
5970/**
5971 * _scsih_probe_devices - probing for devices
5972 * @ioc: per adapter object
5973 *
5974 * Called during initial loading of the driver.
5975 */
5976static void
5977_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5978{
5979 u16 volume_mapping_flags =
5980 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5981 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5982
5983 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5984 return; /* return when IOC doesn't support initiator mode */
5985
5986 _scsih_probe_boot_devices(ioc);
5987
5988 if (ioc->ir_firmware) {
5989 if ((volume_mapping_flags &
5990 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5991 _scsih_probe_sas(ioc);
5992 _scsih_probe_raid(ioc);
5993 } else {
5994 _scsih_probe_raid(ioc);
5995 _scsih_probe_sas(ioc);
5996 }
5997 } else
5998 _scsih_probe_sas(ioc);
5999}
6000
6001/**
Eric Moored5d135b2009-05-18 13:02:08 -06006002 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006003 * @pdev: PCI device struct
6004 * @id: pci device id
6005 *
6006 * Returns 0 success, anything else error.
6007 */
6008static int
Eric Moored5d135b2009-05-18 13:02:08 -06006009_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006010{
6011 struct MPT2SAS_ADAPTER *ioc;
6012 struct Scsi_Host *shost;
6013
6014 shost = scsi_host_alloc(&scsih_driver_template,
6015 sizeof(struct MPT2SAS_ADAPTER));
6016 if (!shost)
6017 return -ENODEV;
6018
6019 /* init local params */
6020 ioc = shost_priv(shost);
6021 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6022 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006023 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006024 ioc->shost = shost;
6025 ioc->id = mpt_ids++;
6026 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6027 ioc->pdev = pdev;
6028 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6029 ioc->tm_cb_idx = tm_cb_idx;
6030 ioc->ctl_cb_idx = ctl_cb_idx;
6031 ioc->base_cb_idx = base_cb_idx;
6032 ioc->transport_cb_idx = transport_cb_idx;
6033 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306034 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6035 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006036 ioc->logging_level = logging_level;
6037 /* misc semaphores and spin locks */
6038 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6039 spin_lock_init(&ioc->scsi_lookup_lock);
6040 spin_lock_init(&ioc->sas_device_lock);
6041 spin_lock_init(&ioc->sas_node_lock);
6042 spin_lock_init(&ioc->fw_event_lock);
6043 spin_lock_init(&ioc->raid_device_lock);
6044
6045 INIT_LIST_HEAD(&ioc->sas_device_list);
6046 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6047 INIT_LIST_HEAD(&ioc->sas_expander_list);
6048 INIT_LIST_HEAD(&ioc->fw_event_list);
6049 INIT_LIST_HEAD(&ioc->raid_device_list);
6050 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306051 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Eric Moore635374e2009-03-09 01:21:12 -06006052
6053 /* init shost parameters */
6054 shost->max_cmd_len = 16;
6055 shost->max_lun = max_lun;
6056 shost->transportt = mpt2sas_transport_template;
6057 shost->unique_id = ioc->id;
6058
6059 if ((scsi_add_host(shost, &pdev->dev))) {
6060 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6061 ioc->name, __FILE__, __LINE__, __func__);
6062 list_del(&ioc->list);
6063 goto out_add_shost_fail;
6064 }
6065
Eric Moore3c621b32009-05-18 12:59:41 -06006066 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6067 | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306068 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006069
Eric Moore635374e2009-03-09 01:21:12 -06006070 /* event thread */
6071 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6072 "fw_event%d", ioc->id);
6073 ioc->firmware_event_thread = create_singlethread_workqueue(
6074 ioc->firmware_event_name);
6075 if (!ioc->firmware_event_thread) {
6076 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6077 ioc->name, __FILE__, __LINE__, __func__);
6078 goto out_thread_fail;
6079 }
6080
6081 ioc->wait_for_port_enable_to_complete = 1;
6082 if ((mpt2sas_base_attach(ioc))) {
6083 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6084 ioc->name, __FILE__, __LINE__, __func__);
6085 goto out_attach_fail;
6086 }
6087
6088 ioc->wait_for_port_enable_to_complete = 0;
6089 _scsih_probe_devices(ioc);
6090 return 0;
6091
6092 out_attach_fail:
6093 destroy_workqueue(ioc->firmware_event_thread);
6094 out_thread_fail:
6095 list_del(&ioc->list);
6096 scsi_remove_host(shost);
6097 out_add_shost_fail:
6098 return -ENODEV;
6099}
6100
6101#ifdef CONFIG_PM
6102/**
Eric Moored5d135b2009-05-18 13:02:08 -06006103 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006104 * @pdev: PCI device struct
6105 * @state: PM state change to (usually PCI_D3)
6106 *
6107 * Returns 0 success, anything else error.
6108 */
6109static int
Eric Moored5d135b2009-05-18 13:02:08 -06006110_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006111{
6112 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6113 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6114 u32 device_state;
6115
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306116 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006117 flush_scheduled_work();
6118 scsi_block_requests(shost);
6119 device_state = pci_choose_state(pdev, state);
6120 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6121 "operating state [D%d]\n", ioc->name, pdev,
6122 pci_name(pdev), device_state);
6123
6124 mpt2sas_base_free_resources(ioc);
6125 pci_save_state(pdev);
6126 pci_disable_device(pdev);
6127 pci_set_power_state(pdev, device_state);
6128 return 0;
6129}
6130
6131/**
Eric Moored5d135b2009-05-18 13:02:08 -06006132 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006133 * @pdev: PCI device struct
6134 *
6135 * Returns 0 success, anything else error.
6136 */
6137static int
Eric Moored5d135b2009-05-18 13:02:08 -06006138_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006139{
6140 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6141 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6142 u32 device_state = pdev->current_state;
6143 int r;
6144
6145 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6146 "operating state [D%d]\n", ioc->name, pdev,
6147 pci_name(pdev), device_state);
6148
6149 pci_set_power_state(pdev, PCI_D0);
6150 pci_enable_wake(pdev, PCI_D0, 0);
6151 pci_restore_state(pdev);
6152 ioc->pdev = pdev;
6153 r = mpt2sas_base_map_resources(ioc);
6154 if (r)
6155 return r;
6156
6157 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6158 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306159 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006160 return 0;
6161}
6162#endif /* CONFIG_PM */
6163
6164
6165static struct pci_driver scsih_driver = {
6166 .name = MPT2SAS_DRIVER_NAME,
6167 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06006168 .probe = _scsih_probe,
6169 .remove = __devexit_p(_scsih_remove),
Eric Moore635374e2009-03-09 01:21:12 -06006170#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06006171 .suspend = _scsih_suspend,
6172 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06006173#endif
6174};
6175
6176
6177/**
Eric Moored5d135b2009-05-18 13:02:08 -06006178 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06006179 *
6180 * Returns 0 success, anything else error.
6181 */
6182static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06006183_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06006184{
6185 int error;
6186
6187 mpt_ids = 0;
6188 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
6189 MPT2SAS_DRIVER_VERSION);
6190
6191 mpt2sas_transport_template =
6192 sas_attach_transport(&mpt2sas_transport_functions);
6193 if (!mpt2sas_transport_template)
6194 return -ENODEV;
6195
6196 mpt2sas_base_initialize_callback_handler();
6197
6198 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06006199 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06006200
6201 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06006202 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06006203
6204 /* base internal commands callback handler */
6205 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
6206
6207 /* transport internal commands callback handler */
6208 transport_cb_idx = mpt2sas_base_register_callback_handler(
6209 mpt2sas_transport_done);
6210
6211 /* configuration page API internal commands callback handler */
6212 config_cb_idx = mpt2sas_base_register_callback_handler(
6213 mpt2sas_config_done);
6214
6215 /* ctl module callback handler */
6216 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
6217
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306218 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
6219 _scsih_tm_tr_complete);
6220 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
6221 _scsih_sas_control_complete);
6222
Eric Moore635374e2009-03-09 01:21:12 -06006223 mpt2sas_ctl_init();
6224
6225 error = pci_register_driver(&scsih_driver);
6226 if (error)
6227 sas_release_transport(mpt2sas_transport_template);
6228
6229 return error;
6230}
6231
6232/**
Eric Moored5d135b2009-05-18 13:02:08 -06006233 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06006234 *
6235 * Returns 0 success, anything else error.
6236 */
6237static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06006238_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06006239{
6240 printk(KERN_INFO "mpt2sas version %s unloading\n",
6241 MPT2SAS_DRIVER_VERSION);
6242
6243 pci_unregister_driver(&scsih_driver);
6244
6245 sas_release_transport(mpt2sas_transport_template);
6246 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
6247 mpt2sas_base_release_callback_handler(tm_cb_idx);
6248 mpt2sas_base_release_callback_handler(base_cb_idx);
6249 mpt2sas_base_release_callback_handler(transport_cb_idx);
6250 mpt2sas_base_release_callback_handler(config_cb_idx);
6251 mpt2sas_base_release_callback_handler(ctl_cb_idx);
6252
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306253 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
6254 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
6255
Eric Moore635374e2009-03-09 01:21:12 -06006256 mpt2sas_ctl_exit();
6257}
6258
Eric Moored5d135b2009-05-18 13:02:08 -06006259module_init(_scsih_init);
6260module_exit(_scsih_exit);