Need notification to my application when a SCSI disk gets connected to the system

I am using an application written in C++ on Windows 7, that is used to detect  SAS disks connected to the LSI HBA on my system and to send various SCSI commands to the disk.

Presently to the detect the disks connected to the system, user has to scan the device tree of windows, get the details of the new disks that are added to the system and update my application with those details.
my application uses SetupDiGetClassDevs API with the flag GUID_DEVINTERFACE_DISK for this purpose.

I have a requirement of enabling the application to receive a notification from windows when the disk is connected/removed  to/from  the system. In other words, i need to make my application PNP enabled.  

Is it possible to get a notification when a new disk gets connected to the SAS HBA or removed from the HBA? Does windows 7 provide any  APIS for this purpose?

Can i find some sample code which does this?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Exactly what controller do you have?  This is critical, because certain LSI controllers do not tell the O/S anything when HDDs are added/removed.  So with some controllers, the only way to do the job is to use the LSI API.  As such even if I were to write it for you, I wouldn't be able to release anything but an executable.
raj_ksrtAuthor Commented:

I am using LSI-9200 16e controllers and i am able see some notifications from LSI controller to my mini port driver. Also i had done some search around and found an example application in MFC that receives WM_DEVICECHANGE message when a hard disk is connected or removed from the LSI controller.

My question is whether i can get some call back from OS without using MFC in my app.

Also i think device manager is just an applet. If some controllers don't inform OS about the changes in the connected disk configuration then  how device manager would update its device list accordingly when a disk is added or removed from the system?
Those controllers will not always notify the OS of a change.  In fact, the card doesn't even do the WM_DEVICECHANGE reliably.  You've been lucky (well, perhaps unlucky) that you haven't run into a test scenario when the WM_DEVICECHANGE didn't sense a change.

If your goal is to have an automated trigger under windows that tells you when a device is added/removed in all cases, then forget it.  You have to probe.  I've got the developer's library and there is a low-level controller ioctl that you have to send to it in order to force a rescan.

I don't know how high you scale, but I've seen all sorts of discovery issues when you start having multiple initiator situations and/or expanders, and just even reconnect targets.  The windows device name (\\.\PHYSICALDRIVEn  can change for any arbitrary disk if you disconnect and reconnect also, or the \\.\PHYSICALDRIVEn can even get mapped to a different device on a reboot or even rescan after a disconnect-reconnect).

Bottom line, the answer is not only no, there is no way to do this (accurately), but I expect your existing code is broken unless you don't have to worry about expanders, SATA/SATL encapsulation, multipathing, or SAS targets getting disconnected/reconnected or even moved to a different bay in an expander.

Hint - if you want this to work, no matter what, you need to enable persistent mapping.  I learned that the hard way a few years ago.  Based on my assumption of what you are trying to do, if you don't enable it, you risk talking to the wrong device after a topology change.
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

raj_ksrtAuthor Commented:

Thanks for the reply.  From the above I understand that it is not very viable solution to rely upon WM_DEVICECHANGE message from windows. (although i have seen windows throwing this message fairly consistently in case of disk topology changes on the controller)

Can i know what is persistent mapping , it's purpose and how to enable the same?  Just for information sake.
Persistent mapping is a firmware feature that nails the WWN to a specific device name .  So each target will always have the same \\.\PHYSICALDRIVEx, even through reboots and hardware changes.

I am a LSI developer and so have the API under non-disclosure, and just control it that way through my C code, so am not sure how you can manipulate it without the API, but chances are good such a setting is available through the BIOS.
raj_ksrtAuthor Commented:
I am running some sudden power off test cases on my disk for multiple iterations and with each iteration after the disk is powered on, i am observing that the is getting a new target ID assigned by the the HBA.

so here is my understanding.

1. A device usually has a WWN that is unique to that ID and that doesn't change.

2. The Persistent mapping feature binds the WWN to a specific device name such as \\.\PHYSICALDRIVE0 or  \\.\PHYSICALDRIVE1 hence even when the disk is removed and reconnected or when the system is rebooted the device name will not change. Also the target ID assigned to the drive by the HBA will not change across power cycles of the disk.

Could you please confirm if the understanding is correct? i think that will conclude this topic.
1) True, but a device can have more than one WWN, depending on what is on EVPD pages 80-83h.  Also SATA disks may or may not have a WWN, depending on the version of ATA spec they are compliant with. But if you put a SATA device on an expander and/or attach to a SAS/SATA controller, then it will be given a WWN one way or another.   One must be wary of SATA devices and the WWN may be auto-assigned so that the WWN is a function of nothing more than a bay# in an expander.  

So there is going to be a unique WWN per EXPANDER.  And the  individual device itself will have a unique WWN if SAS.  If SATA,it may or may not have a unique WWN.   (There are also more than one kind of WWN)

WWNs can be overwritten by SAS switches, and through the LSI API.  These don't change the device itself, they just change what is presented to the host OS and/or expanders.

Also since SAS devices are dual ported, you are going to always have 2 WWNs, one per port, SATA are single ported.  Cross referencing can be done by doing EVPD page SCSI inquiries.

2) Persistence mapping actually insures the WWN gets mapped to same bus/target ids, which is subtle difference.  The O/S still has the option to change the mapping, but that typically requires you to add/remove HBAs, or use different HBA ports, or make some sort of hardware change.   Persistence is more appropriately used to make sure that if you are doing multipathing or have to swap out a LSI controller that the mapping will be exactly the same between them. I have seen windows change a device name when persistent mapping is turned on, but I have never seen this happen if the HDD has a drive letter assigned to it.  Sorry I can't be more specific on persistent mapping rules, I never had to really care about them enough in any of my code so never needed to research all the rules of what can and can't happen.  Probably a whitepaper on it somewhere on the internet.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.