mptsas.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  1. /*
  2. * linux/drivers/message/fusion/mptsas.c
  3. * For use with LSI Logic PCI chip/adapter(s)
  4. * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2005 LSI Logic Corporation
  7. * (mailto:mpt_linux_developer@lsil.com)
  8. * Copyright (c) 2005-2006 Dell
  9. */
  10. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  11. /*
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; version 2 of the License.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. NO WARRANTY
  20. THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. solely responsible for determining the appropriateness of using and
  25. distributing the Program and assumes all risks associated with its
  26. exercise of rights under this Agreement, including but not limited to
  27. the risks and costs of program errors, damage to or loss of data,
  28. programs or equipment, and unavailability or interruption of operations.
  29. 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. You should have received a copy of the GNU General Public License
  38. along with this program; if not, write to the Free Software
  39. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. */
  41. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/sched.h>
  47. #include <linux/workqueue.h>
  48. #include <scsi/scsi_cmnd.h>
  49. #include <scsi/scsi_device.h>
  50. #include <scsi/scsi_host.h>
  51. #include <scsi/scsi_transport_sas.h>
  52. #include "mptbase.h"
  53. #include "mptscsih.h"
  54. #define my_NAME "Fusion MPT SAS Host driver"
  55. #define my_VERSION MPT_LINUX_VERSION_COMMON
  56. #define MYNAM "mptsas"
  57. MODULE_AUTHOR(MODULEAUTHOR);
  58. MODULE_DESCRIPTION(my_NAME);
  59. MODULE_LICENSE("GPL");
  60. static int mpt_pq_filter;
  61. module_param(mpt_pq_filter, int, 0);
  62. MODULE_PARM_DESC(mpt_pq_filter,
  63. "Enable peripheral qualifier filter: enable=1 "
  64. "(default=0)");
  65. static int mpt_pt_clear;
  66. module_param(mpt_pt_clear, int, 0);
  67. MODULE_PARM_DESC(mpt_pt_clear,
  68. "Clear persistency table: enable=1 "
  69. "(default=MPTSCSIH_PT_CLEAR=0)");
  70. static int mptsasDoneCtx = -1;
  71. static int mptsasTaskCtx = -1;
  72. static int mptsasInternalCtx = -1; /* Used only for internal commands */
  73. static int mptsasMgmtCtx = -1;
  74. enum mptsas_hotplug_action {
  75. MPTSAS_ADD_DEVICE,
  76. MPTSAS_DEL_DEVICE,
  77. MPTSAS_ADD_RAID,
  78. MPTSAS_DEL_RAID,
  79. };
  80. struct mptsas_hotplug_event {
  81. struct work_struct work;
  82. MPT_ADAPTER *ioc;
  83. enum mptsas_hotplug_action event_type;
  84. u64 sas_address;
  85. u32 channel;
  86. u32 id;
  87. u32 device_info;
  88. u16 handle;
  89. u16 parent_handle;
  90. u8 phy_id;
  91. u8 phys_disk_num;
  92. u8 phys_disk_num_valid;
  93. };
  94. struct mptsas_discovery_event {
  95. struct work_struct work;
  96. MPT_ADAPTER *ioc;
  97. };
  98. /*
  99. * SAS topology structures
  100. *
  101. * The MPT Fusion firmware interface spreads information about the
  102. * SAS topology over many manufacture pages, thus we need some data
  103. * structure to collect it and process it for the SAS transport class.
  104. */
  105. struct mptsas_devinfo {
  106. u16 handle; /* unique id to address this device */
  107. u16 handle_parent; /* unique id to address parent device */
  108. u16 handle_enclosure; /* enclosure identifier of the enclosure */
  109. u16 slot; /* physical slot in enclosure */
  110. u8 phy_id; /* phy number of parent device */
  111. u8 port_id; /* sas physical port this device
  112. is assoc'd with */
  113. u8 id; /* logical target id of this device */
  114. u8 channel; /* logical bus number of this device */
  115. u64 sas_address; /* WWN of this device,
  116. SATA is assigned by HBA,expander */
  117. u32 device_info; /* bitfield detailed info about this device */
  118. };
  119. struct mptsas_phyinfo {
  120. u8 phy_id; /* phy index */
  121. u8 port_id; /* port number this phy is part of */
  122. u8 negotiated_link_rate; /* nego'd link rate for this phy */
  123. u8 hw_link_rate; /* hardware max/min phys link rate */
  124. u8 programmed_link_rate; /* programmed max/min phy link rate */
  125. struct mptsas_devinfo identify; /* point to phy device info */
  126. struct mptsas_devinfo attached; /* point to attached device info */
  127. struct sas_phy *phy;
  128. struct sas_rphy *rphy;
  129. struct scsi_target *starget;
  130. };
  131. struct mptsas_portinfo {
  132. struct list_head list;
  133. u16 handle; /* unique id to address this */
  134. u8 num_phys; /* number of phys */
  135. struct mptsas_phyinfo *phy_info;
  136. };
  137. struct mptsas_enclosure {
  138. u64 enclosure_logical_id; /* The WWN for the enclosure */
  139. u16 enclosure_handle; /* unique id to address this */
  140. u16 flags; /* details enclosure management */
  141. u16 num_slot; /* num slots */
  142. u16 start_slot; /* first slot */
  143. u8 start_id; /* starting logical target id */
  144. u8 start_channel; /* starting logical channel id */
  145. u8 sep_id; /* SEP device logical target id */
  146. u8 sep_channel; /* SEP channel logical channel id */
  147. };
  148. #ifdef SASDEBUG
  149. static void mptsas_print_phy_data(MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
  150. {
  151. printk("---- IO UNIT PAGE 0 ------------\n");
  152. printk("Handle=0x%X\n",
  153. le16_to_cpu(phy_data->AttachedDeviceHandle));
  154. printk("Controller Handle=0x%X\n",
  155. le16_to_cpu(phy_data->ControllerDevHandle));
  156. printk("Port=0x%X\n", phy_data->Port);
  157. printk("Port Flags=0x%X\n", phy_data->PortFlags);
  158. printk("PHY Flags=0x%X\n", phy_data->PhyFlags);
  159. printk("Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate);
  160. printk("Controller PHY Device Info=0x%X\n",
  161. le32_to_cpu(phy_data->ControllerPhyDeviceInfo));
  162. printk("DiscoveryStatus=0x%X\n",
  163. le32_to_cpu(phy_data->DiscoveryStatus));
  164. printk("\n");
  165. }
  166. static void mptsas_print_phy_pg0(SasPhyPage0_t *pg0)
  167. {
  168. __le64 sas_address;
  169. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  170. printk("---- SAS PHY PAGE 0 ------------\n");
  171. printk("Attached Device Handle=0x%X\n",
  172. le16_to_cpu(pg0->AttachedDevHandle));
  173. printk("SAS Address=0x%llX\n",
  174. (unsigned long long)le64_to_cpu(sas_address));
  175. printk("Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier);
  176. printk("Attached Device Info=0x%X\n",
  177. le32_to_cpu(pg0->AttachedDeviceInfo));
  178. printk("Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate);
  179. printk("Change Count=0x%X\n", pg0->ChangeCount);
  180. printk("PHY Info=0x%X\n", le32_to_cpu(pg0->PhyInfo));
  181. printk("\n");
  182. }
  183. static void mptsas_print_phy_pg1(SasPhyPage1_t *pg1)
  184. {
  185. printk("---- SAS PHY PAGE 1 ------------\n");
  186. printk("Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount);
  187. printk("Running Disparity Error Count=0x%x\n",
  188. pg1->RunningDisparityErrorCount);
  189. printk("Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount);
  190. printk("PHY Reset Problem Count=0x%x\n", pg1->PhyResetProblemCount);
  191. printk("\n");
  192. }
  193. static void mptsas_print_device_pg0(SasDevicePage0_t *pg0)
  194. {
  195. __le64 sas_address;
  196. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  197. printk("---- SAS DEVICE PAGE 0 ---------\n");
  198. printk("Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle));
  199. printk("Parent Handle=0x%X\n" ,le16_to_cpu(pg0->ParentDevHandle));
  200. printk("Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle));
  201. printk("Slot=0x%X\n", le16_to_cpu(pg0->Slot));
  202. printk("SAS Address=0x%llX\n", le64_to_cpu(sas_address));
  203. printk("Target ID=0x%X\n", pg0->TargetID);
  204. printk("Bus=0x%X\n", pg0->Bus);
  205. /* The PhyNum field specifies the PHY number of the parent
  206. * device this device is linked to
  207. */
  208. printk("Parent Phy Num=0x%X\n", pg0->PhyNum);
  209. printk("Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus));
  210. printk("Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo));
  211. printk("Flags=0x%X\n", le16_to_cpu(pg0->Flags));
  212. printk("Physical Port=0x%X\n", pg0->PhysicalPort);
  213. printk("\n");
  214. }
  215. static void mptsas_print_expander_pg1(SasExpanderPage1_t *pg1)
  216. {
  217. printk("---- SAS EXPANDER PAGE 1 ------------\n");
  218. printk("Physical Port=0x%X\n", pg1->PhysicalPort);
  219. printk("PHY Identifier=0x%X\n", pg1->PhyIdentifier);
  220. printk("Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate);
  221. printk("Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate);
  222. printk("Hardware Link Rate=0x%X\n", pg1->HwLinkRate);
  223. printk("Owner Device Handle=0x%X\n",
  224. le16_to_cpu(pg1->OwnerDevHandle));
  225. printk("Attached Device Handle=0x%X\n",
  226. le16_to_cpu(pg1->AttachedDevHandle));
  227. }
  228. #else
  229. #define mptsas_print_phy_data(phy_data) do { } while (0)
  230. #define mptsas_print_phy_pg0(pg0) do { } while (0)
  231. #define mptsas_print_phy_pg1(pg1) do { } while (0)
  232. #define mptsas_print_device_pg0(pg0) do { } while (0)
  233. #define mptsas_print_expander_pg1(pg1) do { } while (0)
  234. #endif
  235. static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
  236. {
  237. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  238. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  239. }
  240. static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
  241. {
  242. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  243. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  244. }
  245. /*
  246. * mptsas_find_portinfo_by_handle
  247. *
  248. * This function should be called with the sas_topology_mutex already held
  249. */
  250. static struct mptsas_portinfo *
  251. mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
  252. {
  253. struct mptsas_portinfo *port_info, *rc=NULL;
  254. int i;
  255. list_for_each_entry(port_info, &ioc->sas_topology, list)
  256. for (i = 0; i < port_info->num_phys; i++)
  257. if (port_info->phy_info[i].identify.handle == handle) {
  258. rc = port_info;
  259. goto out;
  260. }
  261. out:
  262. return rc;
  263. }
  264. static int
  265. mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
  266. u32 form, u32 form_specific)
  267. {
  268. ConfigExtendedPageHeader_t hdr;
  269. CONFIGPARMS cfg;
  270. SasEnclosurePage0_t *buffer;
  271. dma_addr_t dma_handle;
  272. int error;
  273. __le64 le_identifier;
  274. memset(&hdr, 0, sizeof(hdr));
  275. hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
  276. hdr.PageNumber = 0;
  277. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  278. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
  279. cfg.cfghdr.ehdr = &hdr;
  280. cfg.physAddr = -1;
  281. cfg.pageAddr = form + form_specific;
  282. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  283. cfg.dir = 0; /* read */
  284. cfg.timeout = 10;
  285. error = mpt_config(ioc, &cfg);
  286. if (error)
  287. goto out;
  288. if (!hdr.ExtPageLength) {
  289. error = -ENXIO;
  290. goto out;
  291. }
  292. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  293. &dma_handle);
  294. if (!buffer) {
  295. error = -ENOMEM;
  296. goto out;
  297. }
  298. cfg.physAddr = dma_handle;
  299. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  300. error = mpt_config(ioc, &cfg);
  301. if (error)
  302. goto out_free_consistent;
  303. /* save config data */
  304. memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
  305. enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
  306. enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
  307. enclosure->flags = le16_to_cpu(buffer->Flags);
  308. enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
  309. enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
  310. enclosure->start_id = buffer->StartTargetID;
  311. enclosure->start_channel = buffer->StartBus;
  312. enclosure->sep_id = buffer->SEPTargetID;
  313. enclosure->sep_channel = buffer->SEPBus;
  314. out_free_consistent:
  315. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  316. buffer, dma_handle);
  317. out:
  318. return error;
  319. }
  320. static int
  321. mptsas_slave_configure(struct scsi_device *sdev)
  322. {
  323. struct Scsi_Host *host = sdev->host;
  324. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  325. /*
  326. * RAID volumes placed beyond the last expected port.
  327. * Ignore sending sas mode pages in that case..
  328. */
  329. if (sdev->channel < hd->ioc->num_ports)
  330. sas_read_port_mode_page(sdev);
  331. return mptscsih_slave_configure(sdev);
  332. }
  333. /*
  334. * This is pretty ugly. We will be able to seriously clean it up
  335. * once the DV code in mptscsih goes away and we can properly
  336. * implement ->target_alloc.
  337. */
  338. static int
  339. mptsas_slave_alloc(struct scsi_device *sdev)
  340. {
  341. struct Scsi_Host *host = sdev->host;
  342. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  343. struct sas_rphy *rphy;
  344. struct mptsas_portinfo *p;
  345. VirtTarget *vtarget;
  346. VirtDevice *vdev;
  347. struct scsi_target *starget;
  348. u32 target_id;
  349. int i;
  350. vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  351. if (!vdev) {
  352. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  353. hd->ioc->name, sizeof(VirtDevice));
  354. return -ENOMEM;
  355. }
  356. sdev->hostdata = vdev;
  357. starget = scsi_target(sdev);
  358. vtarget = starget->hostdata;
  359. vtarget->ioc_id = hd->ioc->id;
  360. vdev->vtarget = vtarget;
  361. if (vtarget->num_luns == 0) {
  362. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES|MPT_TARGET_FLAGS_VALID_INQUIRY;
  363. hd->Targets[sdev->id] = vtarget;
  364. }
  365. /*
  366. RAID volumes placed beyond the last expected port.
  367. */
  368. if (sdev->channel == hd->ioc->num_ports) {
  369. target_id = sdev->id;
  370. vtarget->bus_id = 0;
  371. vdev->lun = 0;
  372. goto out;
  373. }
  374. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  375. mutex_lock(&hd->ioc->sas_topology_mutex);
  376. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  377. for (i = 0; i < p->num_phys; i++) {
  378. if (p->phy_info[i].attached.sas_address ==
  379. rphy->identify.sas_address) {
  380. target_id = p->phy_info[i].attached.id;
  381. vtarget->bus_id = p->phy_info[i].attached.channel;
  382. vdev->lun = sdev->lun;
  383. p->phy_info[i].starget = sdev->sdev_target;
  384. /*
  385. * Exposing hidden disk (RAID)
  386. */
  387. if (mptscsih_is_phys_disk(hd->ioc, target_id)) {
  388. target_id = mptscsih_raid_id_to_num(hd,
  389. target_id);
  390. vdev->vtarget->tflags |=
  391. MPT_TARGET_FLAGS_RAID_COMPONENT;
  392. sdev->no_uld_attach = 1;
  393. }
  394. mutex_unlock(&hd->ioc->sas_topology_mutex);
  395. goto out;
  396. }
  397. }
  398. }
  399. mutex_unlock(&hd->ioc->sas_topology_mutex);
  400. kfree(vdev);
  401. return -ENXIO;
  402. out:
  403. vtarget->target_id = target_id;
  404. vtarget->num_luns++;
  405. return 0;
  406. }
  407. static void
  408. mptsas_slave_destroy(struct scsi_device *sdev)
  409. {
  410. struct Scsi_Host *host = sdev->host;
  411. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  412. VirtDevice *vdev;
  413. /*
  414. * Issue target reset to flush firmware outstanding commands.
  415. */
  416. vdev = sdev->hostdata;
  417. if (vdev->configured_lun){
  418. if (mptscsih_TMHandler(hd,
  419. MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
  420. vdev->vtarget->bus_id,
  421. vdev->vtarget->target_id,
  422. 0, 0, 5 /* 5 second timeout */)
  423. < 0){
  424. /* The TM request failed!
  425. * Fatal error case.
  426. */
  427. printk(MYIOC_s_WARN_FMT
  428. "Error processing TaskMgmt id=%d TARGET_RESET\n",
  429. hd->ioc->name,
  430. vdev->vtarget->target_id);
  431. hd->tmPending = 0;
  432. hd->tmState = TM_STATE_NONE;
  433. }
  434. }
  435. mptscsih_slave_destroy(sdev);
  436. }
  437. static struct scsi_host_template mptsas_driver_template = {
  438. .module = THIS_MODULE,
  439. .proc_name = "mptsas",
  440. .proc_info = mptscsih_proc_info,
  441. .name = "MPT SPI Host",
  442. .info = mptscsih_info,
  443. .queuecommand = mptscsih_qcmd,
  444. .target_alloc = mptscsih_target_alloc,
  445. .slave_alloc = mptsas_slave_alloc,
  446. .slave_configure = mptsas_slave_configure,
  447. .target_destroy = mptscsih_target_destroy,
  448. .slave_destroy = mptsas_slave_destroy,
  449. .change_queue_depth = mptscsih_change_queue_depth,
  450. .eh_abort_handler = mptscsih_abort,
  451. .eh_device_reset_handler = mptscsih_dev_reset,
  452. .eh_bus_reset_handler = mptscsih_bus_reset,
  453. .eh_host_reset_handler = mptscsih_host_reset,
  454. .bios_param = mptscsih_bios_param,
  455. .can_queue = MPT_FC_CAN_QUEUE,
  456. .this_id = -1,
  457. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  458. .max_sectors = 8192,
  459. .cmd_per_lun = 7,
  460. .use_clustering = ENABLE_CLUSTERING,
  461. };
  462. static int mptsas_get_linkerrors(struct sas_phy *phy)
  463. {
  464. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  465. ConfigExtendedPageHeader_t hdr;
  466. CONFIGPARMS cfg;
  467. SasPhyPage1_t *buffer;
  468. dma_addr_t dma_handle;
  469. int error;
  470. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  471. hdr.ExtPageLength = 0;
  472. hdr.PageNumber = 1 /* page number 1*/;
  473. hdr.Reserved1 = 0;
  474. hdr.Reserved2 = 0;
  475. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  476. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  477. cfg.cfghdr.ehdr = &hdr;
  478. cfg.physAddr = -1;
  479. cfg.pageAddr = phy->identify.phy_identifier;
  480. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  481. cfg.dir = 0; /* read */
  482. cfg.timeout = 10;
  483. error = mpt_config(ioc, &cfg);
  484. if (error)
  485. return error;
  486. if (!hdr.ExtPageLength)
  487. return -ENXIO;
  488. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  489. &dma_handle);
  490. if (!buffer)
  491. return -ENOMEM;
  492. cfg.physAddr = dma_handle;
  493. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  494. error = mpt_config(ioc, &cfg);
  495. if (error)
  496. goto out_free_consistent;
  497. mptsas_print_phy_pg1(buffer);
  498. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  499. phy->running_disparity_error_count =
  500. le32_to_cpu(buffer->RunningDisparityErrorCount);
  501. phy->loss_of_dword_sync_count =
  502. le32_to_cpu(buffer->LossDwordSynchCount);
  503. phy->phy_reset_problem_count =
  504. le32_to_cpu(buffer->PhyResetProblemCount);
  505. out_free_consistent:
  506. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  507. buffer, dma_handle);
  508. return error;
  509. }
  510. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  511. MPT_FRAME_HDR *reply)
  512. {
  513. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
  514. if (reply != NULL) {
  515. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
  516. memcpy(ioc->sas_mgmt.reply, reply,
  517. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  518. }
  519. complete(&ioc->sas_mgmt.done);
  520. return 1;
  521. }
  522. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  523. {
  524. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  525. SasIoUnitControlRequest_t *req;
  526. SasIoUnitControlReply_t *reply;
  527. MPT_FRAME_HDR *mf;
  528. MPIHeader_t *hdr;
  529. unsigned long timeleft;
  530. int error = -ERESTARTSYS;
  531. /* not implemented for expanders */
  532. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  533. return -ENXIO;
  534. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  535. goto out;
  536. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  537. if (!mf) {
  538. error = -ENOMEM;
  539. goto out_unlock;
  540. }
  541. hdr = (MPIHeader_t *) mf;
  542. req = (SasIoUnitControlRequest_t *)mf;
  543. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  544. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  545. req->MsgContext = hdr->MsgContext;
  546. req->Operation = hard_reset ?
  547. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  548. req->PhyNum = phy->identify.phy_identifier;
  549. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  550. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  551. 10 * HZ);
  552. if (!timeleft) {
  553. /* On timeout reset the board */
  554. mpt_free_msg_frame(ioc, mf);
  555. mpt_HardResetHandler(ioc, CAN_SLEEP);
  556. error = -ETIMEDOUT;
  557. goto out_unlock;
  558. }
  559. /* a reply frame is expected */
  560. if ((ioc->sas_mgmt.status &
  561. MPT_IOCTL_STATUS_RF_VALID) == 0) {
  562. error = -ENXIO;
  563. goto out_unlock;
  564. }
  565. /* process the completed Reply Message Frame */
  566. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  567. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  568. printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  569. __FUNCTION__,
  570. reply->IOCStatus,
  571. reply->IOCLogInfo);
  572. error = -ENXIO;
  573. goto out_unlock;
  574. }
  575. error = 0;
  576. out_unlock:
  577. mutex_unlock(&ioc->sas_mgmt.mutex);
  578. out:
  579. return error;
  580. }
  581. static int
  582. mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  583. {
  584. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  585. int i, error;
  586. struct mptsas_portinfo *p;
  587. struct mptsas_enclosure enclosure_info;
  588. u64 enclosure_handle;
  589. mutex_lock(&ioc->sas_topology_mutex);
  590. list_for_each_entry(p, &ioc->sas_topology, list) {
  591. for (i = 0; i < p->num_phys; i++) {
  592. if (p->phy_info[i].attached.sas_address ==
  593. rphy->identify.sas_address) {
  594. enclosure_handle = p->phy_info[i].
  595. attached.handle_enclosure;
  596. goto found_info;
  597. }
  598. }
  599. }
  600. mutex_unlock(&ioc->sas_topology_mutex);
  601. return -ENXIO;
  602. found_info:
  603. mutex_unlock(&ioc->sas_topology_mutex);
  604. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  605. error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  606. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  607. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
  608. if (!error)
  609. *identifier = enclosure_info.enclosure_logical_id;
  610. return error;
  611. }
  612. static int
  613. mptsas_get_bay_identifier(struct sas_rphy *rphy)
  614. {
  615. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  616. struct mptsas_portinfo *p;
  617. int i, rc;
  618. mutex_lock(&ioc->sas_topology_mutex);
  619. list_for_each_entry(p, &ioc->sas_topology, list) {
  620. for (i = 0; i < p->num_phys; i++) {
  621. if (p->phy_info[i].attached.sas_address ==
  622. rphy->identify.sas_address) {
  623. rc = p->phy_info[i].attached.slot;
  624. goto out;
  625. }
  626. }
  627. }
  628. rc = -ENXIO;
  629. out:
  630. mutex_unlock(&ioc->sas_topology_mutex);
  631. return rc;
  632. }
  633. static struct sas_function_template mptsas_transport_functions = {
  634. .get_linkerrors = mptsas_get_linkerrors,
  635. .get_enclosure_identifier = mptsas_get_enclosure_identifier,
  636. .get_bay_identifier = mptsas_get_bay_identifier,
  637. .phy_reset = mptsas_phy_reset,
  638. };
  639. static struct scsi_transport_template *mptsas_transport_template;
  640. static int
  641. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  642. {
  643. ConfigExtendedPageHeader_t hdr;
  644. CONFIGPARMS cfg;
  645. SasIOUnitPage0_t *buffer;
  646. dma_addr_t dma_handle;
  647. int error, i;
  648. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  649. hdr.ExtPageLength = 0;
  650. hdr.PageNumber = 0;
  651. hdr.Reserved1 = 0;
  652. hdr.Reserved2 = 0;
  653. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  654. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  655. cfg.cfghdr.ehdr = &hdr;
  656. cfg.physAddr = -1;
  657. cfg.pageAddr = 0;
  658. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  659. cfg.dir = 0; /* read */
  660. cfg.timeout = 10;
  661. error = mpt_config(ioc, &cfg);
  662. if (error)
  663. goto out;
  664. if (!hdr.ExtPageLength) {
  665. error = -ENXIO;
  666. goto out;
  667. }
  668. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  669. &dma_handle);
  670. if (!buffer) {
  671. error = -ENOMEM;
  672. goto out;
  673. }
  674. cfg.physAddr = dma_handle;
  675. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  676. error = mpt_config(ioc, &cfg);
  677. if (error)
  678. goto out_free_consistent;
  679. port_info->num_phys = buffer->NumPhys;
  680. port_info->phy_info = kcalloc(port_info->num_phys,
  681. sizeof(struct mptsas_phyinfo),GFP_KERNEL);
  682. if (!port_info->phy_info) {
  683. error = -ENOMEM;
  684. goto out_free_consistent;
  685. }
  686. if (port_info->num_phys)
  687. port_info->handle =
  688. le16_to_cpu(buffer->PhyData[0].ControllerDevHandle);
  689. for (i = 0; i < port_info->num_phys; i++) {
  690. mptsas_print_phy_data(&buffer->PhyData[i]);
  691. port_info->phy_info[i].phy_id = i;
  692. port_info->phy_info[i].port_id =
  693. buffer->PhyData[i].Port;
  694. port_info->phy_info[i].negotiated_link_rate =
  695. buffer->PhyData[i].NegotiatedLinkRate;
  696. }
  697. out_free_consistent:
  698. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  699. buffer, dma_handle);
  700. out:
  701. return error;
  702. }
  703. static int
  704. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  705. u32 form, u32 form_specific)
  706. {
  707. ConfigExtendedPageHeader_t hdr;
  708. CONFIGPARMS cfg;
  709. SasPhyPage0_t *buffer;
  710. dma_addr_t dma_handle;
  711. int error;
  712. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  713. hdr.ExtPageLength = 0;
  714. hdr.PageNumber = 0;
  715. hdr.Reserved1 = 0;
  716. hdr.Reserved2 = 0;
  717. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  718. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  719. cfg.cfghdr.ehdr = &hdr;
  720. cfg.dir = 0; /* read */
  721. cfg.timeout = 10;
  722. /* Get Phy Pg 0 for each Phy. */
  723. cfg.physAddr = -1;
  724. cfg.pageAddr = form + form_specific;
  725. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  726. error = mpt_config(ioc, &cfg);
  727. if (error)
  728. goto out;
  729. if (!hdr.ExtPageLength) {
  730. error = -ENXIO;
  731. goto out;
  732. }
  733. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  734. &dma_handle);
  735. if (!buffer) {
  736. error = -ENOMEM;
  737. goto out;
  738. }
  739. cfg.physAddr = dma_handle;
  740. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  741. error = mpt_config(ioc, &cfg);
  742. if (error)
  743. goto out_free_consistent;
  744. mptsas_print_phy_pg0(buffer);
  745. phy_info->hw_link_rate = buffer->HwLinkRate;
  746. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  747. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  748. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  749. out_free_consistent:
  750. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  751. buffer, dma_handle);
  752. out:
  753. return error;
  754. }
  755. static int
  756. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  757. u32 form, u32 form_specific)
  758. {
  759. ConfigExtendedPageHeader_t hdr;
  760. CONFIGPARMS cfg;
  761. SasDevicePage0_t *buffer;
  762. dma_addr_t dma_handle;
  763. __le64 sas_address;
  764. int error;
  765. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  766. hdr.ExtPageLength = 0;
  767. hdr.PageNumber = 0;
  768. hdr.Reserved1 = 0;
  769. hdr.Reserved2 = 0;
  770. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  771. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  772. cfg.cfghdr.ehdr = &hdr;
  773. cfg.pageAddr = form + form_specific;
  774. cfg.physAddr = -1;
  775. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  776. cfg.dir = 0; /* read */
  777. cfg.timeout = 10;
  778. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  779. error = mpt_config(ioc, &cfg);
  780. if (error)
  781. goto out;
  782. if (!hdr.ExtPageLength) {
  783. error = -ENXIO;
  784. goto out;
  785. }
  786. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  787. &dma_handle);
  788. if (!buffer) {
  789. error = -ENOMEM;
  790. goto out;
  791. }
  792. cfg.physAddr = dma_handle;
  793. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  794. error = mpt_config(ioc, &cfg);
  795. if (error)
  796. goto out_free_consistent;
  797. mptsas_print_device_pg0(buffer);
  798. device_info->handle = le16_to_cpu(buffer->DevHandle);
  799. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  800. device_info->handle_enclosure =
  801. le16_to_cpu(buffer->EnclosureHandle);
  802. device_info->slot = le16_to_cpu(buffer->Slot);
  803. device_info->phy_id = buffer->PhyNum;
  804. device_info->port_id = buffer->PhysicalPort;
  805. device_info->id = buffer->TargetID;
  806. device_info->channel = buffer->Bus;
  807. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  808. device_info->sas_address = le64_to_cpu(sas_address);
  809. device_info->device_info =
  810. le32_to_cpu(buffer->DeviceInfo);
  811. out_free_consistent:
  812. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  813. buffer, dma_handle);
  814. out:
  815. return error;
  816. }
  817. static int
  818. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  819. u32 form, u32 form_specific)
  820. {
  821. ConfigExtendedPageHeader_t hdr;
  822. CONFIGPARMS cfg;
  823. SasExpanderPage0_t *buffer;
  824. dma_addr_t dma_handle;
  825. int error;
  826. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  827. hdr.ExtPageLength = 0;
  828. hdr.PageNumber = 0;
  829. hdr.Reserved1 = 0;
  830. hdr.Reserved2 = 0;
  831. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  832. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  833. cfg.cfghdr.ehdr = &hdr;
  834. cfg.physAddr = -1;
  835. cfg.pageAddr = form + form_specific;
  836. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  837. cfg.dir = 0; /* read */
  838. cfg.timeout = 10;
  839. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  840. error = mpt_config(ioc, &cfg);
  841. if (error)
  842. goto out;
  843. if (!hdr.ExtPageLength) {
  844. error = -ENXIO;
  845. goto out;
  846. }
  847. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  848. &dma_handle);
  849. if (!buffer) {
  850. error = -ENOMEM;
  851. goto out;
  852. }
  853. cfg.physAddr = dma_handle;
  854. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  855. error = mpt_config(ioc, &cfg);
  856. if (error)
  857. goto out_free_consistent;
  858. /* save config data */
  859. port_info->num_phys = buffer->NumPhys;
  860. port_info->handle = le16_to_cpu(buffer->DevHandle);
  861. port_info->phy_info = kcalloc(port_info->num_phys,
  862. sizeof(struct mptsas_phyinfo),GFP_KERNEL);
  863. if (!port_info->phy_info) {
  864. error = -ENOMEM;
  865. goto out_free_consistent;
  866. }
  867. out_free_consistent:
  868. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  869. buffer, dma_handle);
  870. out:
  871. return error;
  872. }
  873. static int
  874. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  875. u32 form, u32 form_specific)
  876. {
  877. ConfigExtendedPageHeader_t hdr;
  878. CONFIGPARMS cfg;
  879. SasExpanderPage1_t *buffer;
  880. dma_addr_t dma_handle;
  881. int error;
  882. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  883. hdr.ExtPageLength = 0;
  884. hdr.PageNumber = 1;
  885. hdr.Reserved1 = 0;
  886. hdr.Reserved2 = 0;
  887. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  888. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  889. cfg.cfghdr.ehdr = &hdr;
  890. cfg.physAddr = -1;
  891. cfg.pageAddr = form + form_specific;
  892. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  893. cfg.dir = 0; /* read */
  894. cfg.timeout = 10;
  895. error = mpt_config(ioc, &cfg);
  896. if (error)
  897. goto out;
  898. if (!hdr.ExtPageLength) {
  899. error = -ENXIO;
  900. goto out;
  901. }
  902. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  903. &dma_handle);
  904. if (!buffer) {
  905. error = -ENOMEM;
  906. goto out;
  907. }
  908. cfg.physAddr = dma_handle;
  909. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  910. error = mpt_config(ioc, &cfg);
  911. if (error)
  912. goto out_free_consistent;
  913. mptsas_print_expander_pg1(buffer);
  914. /* save config data */
  915. phy_info->phy_id = buffer->PhyIdentifier;
  916. phy_info->port_id = buffer->PhysicalPort;
  917. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  918. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  919. phy_info->hw_link_rate = buffer->HwLinkRate;
  920. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  921. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  922. out_free_consistent:
  923. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  924. buffer, dma_handle);
  925. out:
  926. return error;
  927. }
  928. /*
  929. * Returns true if there is a scsi end device
  930. */
  931. static inline int
  932. mptsas_is_end_device(struct mptsas_devinfo * attached)
  933. {
  934. if ((attached->handle) &&
  935. (attached->device_info &
  936. MPI_SAS_DEVICE_INFO_END_DEVICE) &&
  937. ((attached->device_info &
  938. MPI_SAS_DEVICE_INFO_SSP_TARGET) |
  939. (attached->device_info &
  940. MPI_SAS_DEVICE_INFO_STP_TARGET) |
  941. (attached->device_info &
  942. MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
  943. return 1;
  944. else
  945. return 0;
  946. }
  947. static void
  948. mptsas_parse_device_info(struct sas_identify *identify,
  949. struct mptsas_devinfo *device_info)
  950. {
  951. u16 protocols;
  952. identify->sas_address = device_info->sas_address;
  953. identify->phy_identifier = device_info->phy_id;
  954. /*
  955. * Fill in Phy Initiator Port Protocol.
  956. * Bits 6:3, more than one bit can be set, fall through cases.
  957. */
  958. protocols = device_info->device_info & 0x78;
  959. identify->initiator_port_protocols = 0;
  960. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  961. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  962. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  963. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  964. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  965. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  966. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  967. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  968. /*
  969. * Fill in Phy Target Port Protocol.
  970. * Bits 10:7, more than one bit can be set, fall through cases.
  971. */
  972. protocols = device_info->device_info & 0x780;
  973. identify->target_port_protocols = 0;
  974. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  975. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  976. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  977. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  978. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  979. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  980. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  981. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  982. /*
  983. * Fill in Attached device type.
  984. */
  985. switch (device_info->device_info &
  986. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  987. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  988. identify->device_type = SAS_PHY_UNUSED;
  989. break;
  990. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  991. identify->device_type = SAS_END_DEVICE;
  992. break;
  993. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  994. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  995. break;
  996. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  997. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  998. break;
  999. }
  1000. }
  1001. static int mptsas_probe_one_phy(struct device *dev,
  1002. struct mptsas_phyinfo *phy_info, int index, int local)
  1003. {
  1004. MPT_ADAPTER *ioc;
  1005. struct sas_phy *phy;
  1006. int error;
  1007. if (!dev)
  1008. return -ENODEV;
  1009. if (!phy_info->phy) {
  1010. phy = sas_phy_alloc(dev, index);
  1011. if (!phy)
  1012. return -ENOMEM;
  1013. } else
  1014. phy = phy_info->phy;
  1015. phy->port_identifier = phy_info->port_id;
  1016. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  1017. /*
  1018. * Set Negotiated link rate.
  1019. */
  1020. switch (phy_info->negotiated_link_rate) {
  1021. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  1022. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  1023. break;
  1024. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  1025. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  1026. break;
  1027. case MPI_SAS_IOUNIT0_RATE_1_5:
  1028. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1029. break;
  1030. case MPI_SAS_IOUNIT0_RATE_3_0:
  1031. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1032. break;
  1033. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  1034. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  1035. default:
  1036. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  1037. break;
  1038. }
  1039. /*
  1040. * Set Max hardware link rate.
  1041. */
  1042. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1043. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  1044. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1045. break;
  1046. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1047. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1048. break;
  1049. default:
  1050. break;
  1051. }
  1052. /*
  1053. * Set Max programmed link rate.
  1054. */
  1055. switch (phy_info->programmed_link_rate &
  1056. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1057. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  1058. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1059. break;
  1060. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1061. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1062. break;
  1063. default:
  1064. break;
  1065. }
  1066. /*
  1067. * Set Min hardware link rate.
  1068. */
  1069. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  1070. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  1071. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1072. break;
  1073. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1074. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1075. break;
  1076. default:
  1077. break;
  1078. }
  1079. /*
  1080. * Set Min programmed link rate.
  1081. */
  1082. switch (phy_info->programmed_link_rate &
  1083. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  1084. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  1085. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1086. break;
  1087. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1088. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1089. break;
  1090. default:
  1091. break;
  1092. }
  1093. if (!phy_info->phy) {
  1094. if (local)
  1095. phy->local_attached = 1;
  1096. error = sas_phy_add(phy);
  1097. if (error) {
  1098. sas_phy_free(phy);
  1099. return error;
  1100. }
  1101. phy_info->phy = phy;
  1102. }
  1103. if ((phy_info->attached.handle) &&
  1104. (!phy_info->rphy)) {
  1105. struct sas_rphy *rphy;
  1106. struct sas_identify identify;
  1107. ioc = phy_to_ioc(phy_info->phy);
  1108. /*
  1109. * Let the hotplug_work thread handle processing
  1110. * the adding/removing of devices that occur
  1111. * after start of day.
  1112. */
  1113. if (ioc->sas_discovery_runtime &&
  1114. mptsas_is_end_device(&phy_info->attached))
  1115. return 0;
  1116. mptsas_parse_device_info(&identify, &phy_info->attached);
  1117. switch (identify.device_type) {
  1118. case SAS_END_DEVICE:
  1119. rphy = sas_end_device_alloc(phy);
  1120. break;
  1121. case SAS_EDGE_EXPANDER_DEVICE:
  1122. case SAS_FANOUT_EXPANDER_DEVICE:
  1123. rphy = sas_expander_alloc(phy, identify.device_type);
  1124. break;
  1125. default:
  1126. rphy = NULL;
  1127. break;
  1128. }
  1129. if (!rphy)
  1130. return 0; /* non-fatal: an rphy can be added later */
  1131. rphy->identify = identify;
  1132. error = sas_rphy_add(rphy);
  1133. if (error) {
  1134. sas_rphy_free(rphy);
  1135. return error;
  1136. }
  1137. phy_info->rphy = rphy;
  1138. }
  1139. return 0;
  1140. }
  1141. static int
  1142. mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
  1143. {
  1144. struct mptsas_portinfo *port_info, *hba;
  1145. u32 handle = 0xFFFF;
  1146. int error = -ENOMEM, i;
  1147. hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1148. if (! hba)
  1149. goto out;
  1150. error = mptsas_sas_io_unit_pg0(ioc, hba);
  1151. if (error)
  1152. goto out_free_port_info;
  1153. mutex_lock(&ioc->sas_topology_mutex);
  1154. port_info = mptsas_find_portinfo_by_handle(ioc, hba->handle);
  1155. if (!port_info) {
  1156. port_info = hba;
  1157. list_add_tail(&port_info->list, &ioc->sas_topology);
  1158. } else {
  1159. port_info->handle = hba->handle;
  1160. for (i = 0; i < hba->num_phys; i++)
  1161. port_info->phy_info[i].negotiated_link_rate =
  1162. hba->phy_info[i].negotiated_link_rate;
  1163. if (hba->phy_info)
  1164. kfree(hba->phy_info);
  1165. kfree(hba);
  1166. hba = NULL;
  1167. }
  1168. mutex_unlock(&ioc->sas_topology_mutex);
  1169. ioc->num_ports = port_info->num_phys;
  1170. for (i = 0; i < port_info->num_phys; i++) {
  1171. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  1172. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  1173. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  1174. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  1175. (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
  1176. MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
  1177. port_info->phy_info[i].identify.phy_id =
  1178. port_info->phy_info[i].phy_id;
  1179. handle = port_info->phy_info[i].identify.handle;
  1180. if (port_info->phy_info[i].attached.handle) {
  1181. mptsas_sas_device_pg0(ioc,
  1182. &port_info->phy_info[i].attached,
  1183. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1184. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1185. port_info->phy_info[i].attached.handle);
  1186. }
  1187. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  1188. &port_info->phy_info[i], ioc->sas_index, 1);
  1189. ioc->sas_index++;
  1190. }
  1191. return 0;
  1192. out_free_port_info:
  1193. if (hba)
  1194. kfree(hba);
  1195. out:
  1196. return error;
  1197. }
  1198. static int
  1199. mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
  1200. {
  1201. struct mptsas_portinfo *port_info, *p, *ex;
  1202. int error = -ENOMEM, i, j;
  1203. ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1204. if (!ex)
  1205. goto out;
  1206. error = mptsas_sas_expander_pg0(ioc, ex,
  1207. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  1208. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
  1209. if (error)
  1210. goto out_free_port_info;
  1211. *handle = ex->handle;
  1212. mutex_lock(&ioc->sas_topology_mutex);
  1213. port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
  1214. if (!port_info) {
  1215. port_info = ex;
  1216. list_add_tail(&port_info->list, &ioc->sas_topology);
  1217. } else {
  1218. port_info->handle = ex->handle;
  1219. if (ex->phy_info)
  1220. kfree(ex->phy_info);
  1221. kfree(ex);
  1222. ex = NULL;
  1223. }
  1224. mutex_unlock(&ioc->sas_topology_mutex);
  1225. for (i = 0; i < port_info->num_phys; i++) {
  1226. struct device *parent;
  1227. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  1228. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  1229. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
  1230. if (port_info->phy_info[i].identify.handle) {
  1231. mptsas_sas_device_pg0(ioc,
  1232. &port_info->phy_info[i].identify,
  1233. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1234. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1235. port_info->phy_info[i].identify.handle);
  1236. port_info->phy_info[i].identify.phy_id =
  1237. port_info->phy_info[i].phy_id;
  1238. }
  1239. if (port_info->phy_info[i].attached.handle) {
  1240. mptsas_sas_device_pg0(ioc,
  1241. &port_info->phy_info[i].attached,
  1242. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1243. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1244. port_info->phy_info[i].attached.handle);
  1245. port_info->phy_info[i].attached.phy_id =
  1246. port_info->phy_info[i].phy_id;
  1247. }
  1248. /*
  1249. * If we find a parent port handle this expander is
  1250. * attached to another expander, else it hangs of the
  1251. * HBA phys.
  1252. */
  1253. parent = &ioc->sh->shost_gendev;
  1254. mutex_lock(&ioc->sas_topology_mutex);
  1255. list_for_each_entry(p, &ioc->sas_topology, list) {
  1256. for (j = 0; j < p->num_phys; j++) {
  1257. if (port_info->phy_info[i].identify.handle ==
  1258. p->phy_info[j].attached.handle)
  1259. parent = &p->phy_info[j].rphy->dev;
  1260. }
  1261. }
  1262. mutex_unlock(&ioc->sas_topology_mutex);
  1263. mptsas_probe_one_phy(parent, &port_info->phy_info[i],
  1264. ioc->sas_index, 0);
  1265. ioc->sas_index++;
  1266. }
  1267. return 0;
  1268. out_free_port_info:
  1269. if (ex) {
  1270. if (ex->phy_info)
  1271. kfree(ex->phy_info);
  1272. kfree(ex);
  1273. }
  1274. out:
  1275. return error;
  1276. }
  1277. /*
  1278. * mptsas_delete_expander_phys
  1279. *
  1280. *
  1281. * This will traverse topology, and remove expanders
  1282. * that are no longer present
  1283. */
  1284. static void
  1285. mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
  1286. {
  1287. struct mptsas_portinfo buffer;
  1288. struct mptsas_portinfo *port_info, *n, *parent;
  1289. int i;
  1290. mutex_lock(&ioc->sas_topology_mutex);
  1291. list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
  1292. if (port_info->phy_info &&
  1293. (!(port_info->phy_info[0].identify.device_info &
  1294. MPI_SAS_DEVICE_INFO_SMP_TARGET)))
  1295. continue;
  1296. if (mptsas_sas_expander_pg0(ioc, &buffer,
  1297. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  1298. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), port_info->handle)) {
  1299. /*
  1300. * Obtain the port_info instance to the parent port
  1301. */
  1302. parent = mptsas_find_portinfo_by_handle(ioc,
  1303. port_info->phy_info[0].identify.handle_parent);
  1304. if (!parent)
  1305. goto next_port;
  1306. /*
  1307. * Delete rphys in the parent that point
  1308. * to this expander. The transport layer will
  1309. * cleanup all the children.
  1310. */
  1311. for (i = 0; i < parent->num_phys; i++) {
  1312. if ((!parent->phy_info[i].rphy) ||
  1313. (parent->phy_info[i].attached.sas_address !=
  1314. port_info->phy_info[i].identify.sas_address))
  1315. continue;
  1316. sas_rphy_delete(parent->phy_info[i].rphy);
  1317. memset(&parent->phy_info[i].attached, 0,
  1318. sizeof(struct mptsas_devinfo));
  1319. parent->phy_info[i].rphy = NULL;
  1320. parent->phy_info[i].starget = NULL;
  1321. }
  1322. next_port:
  1323. list_del(&port_info->list);
  1324. if (port_info->phy_info)
  1325. kfree(port_info->phy_info);
  1326. kfree(port_info);
  1327. }
  1328. /*
  1329. * Free this memory allocated from inside
  1330. * mptsas_sas_expander_pg0
  1331. */
  1332. if (buffer.phy_info)
  1333. kfree(buffer.phy_info);
  1334. }
  1335. mutex_unlock(&ioc->sas_topology_mutex);
  1336. }
  1337. /*
  1338. * Start of day discovery
  1339. */
  1340. static void
  1341. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  1342. {
  1343. u32 handle = 0xFFFF;
  1344. int i;
  1345. mutex_lock(&ioc->sas_discovery_mutex);
  1346. mptsas_probe_hba_phys(ioc);
  1347. while (!mptsas_probe_expander_phys(ioc, &handle))
  1348. ;
  1349. /*
  1350. Reporting RAID volumes.
  1351. */
  1352. if (!ioc->raid_data.pIocPg2)
  1353. goto out;
  1354. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  1355. goto out;
  1356. for (i=0; i<ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  1357. scsi_add_device(ioc->sh, ioc->num_ports,
  1358. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  1359. }
  1360. out:
  1361. mutex_unlock(&ioc->sas_discovery_mutex);
  1362. }
  1363. /*
  1364. * Work queue thread to handle Runtime discovery
  1365. * Mere purpose is the hot add/delete of expanders
  1366. */
  1367. static void
  1368. mptscsih_discovery_work(void * arg)
  1369. {
  1370. struct mptsas_discovery_event *ev = arg;
  1371. MPT_ADAPTER *ioc = ev->ioc;
  1372. u32 handle = 0xFFFF;
  1373. mutex_lock(&ioc->sas_discovery_mutex);
  1374. ioc->sas_discovery_runtime=1;
  1375. mptsas_delete_expander_phys(ioc);
  1376. mptsas_probe_hba_phys(ioc);
  1377. while (!mptsas_probe_expander_phys(ioc, &handle))
  1378. ;
  1379. kfree(ev);
  1380. ioc->sas_discovery_runtime=0;
  1381. mutex_unlock(&ioc->sas_discovery_mutex);
  1382. }
  1383. static struct mptsas_phyinfo *
  1384. mptsas_find_phyinfo_by_parent(MPT_ADAPTER *ioc, u16 parent_handle, u8 phy_id)
  1385. {
  1386. struct mptsas_portinfo *port_info;
  1387. struct mptsas_devinfo device_info;
  1388. struct mptsas_phyinfo *phy_info = NULL;
  1389. int i, error;
  1390. /*
  1391. * Retrieve the parent sas_address
  1392. */
  1393. error = mptsas_sas_device_pg0(ioc, &device_info,
  1394. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1395. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1396. parent_handle);
  1397. if (error)
  1398. return NULL;
  1399. /*
  1400. * The phy_info structures are never deallocated during lifetime of
  1401. * a host, so the code below is safe without additional refcounting.
  1402. */
  1403. mutex_lock(&ioc->sas_topology_mutex);
  1404. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  1405. for (i = 0; i < port_info->num_phys; i++) {
  1406. if (port_info->phy_info[i].identify.sas_address ==
  1407. device_info.sas_address &&
  1408. port_info->phy_info[i].phy_id == phy_id) {
  1409. phy_info = &port_info->phy_info[i];
  1410. break;
  1411. }
  1412. }
  1413. }
  1414. mutex_unlock(&ioc->sas_topology_mutex);
  1415. return phy_info;
  1416. }
  1417. static struct mptsas_phyinfo *
  1418. mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
  1419. {
  1420. struct mptsas_portinfo *port_info;
  1421. struct mptsas_phyinfo *phy_info = NULL;
  1422. int i;
  1423. /*
  1424. * The phy_info structures are never deallocated during lifetime of
  1425. * a host, so the code below is safe without additional refcounting.
  1426. */
  1427. mutex_lock(&ioc->sas_topology_mutex);
  1428. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  1429. for (i = 0; i < port_info->num_phys; i++)
  1430. if (mptsas_is_end_device(&port_info->phy_info[i].attached))
  1431. if (port_info->phy_info[i].attached.id == id) {
  1432. phy_info = &port_info->phy_info[i];
  1433. break;
  1434. }
  1435. }
  1436. mutex_unlock(&ioc->sas_topology_mutex);
  1437. return phy_info;
  1438. }
  1439. /*
  1440. * Work queue thread to clear the persitency table
  1441. */
  1442. static void
  1443. mptscsih_sas_persist_clear_table(void * arg)
  1444. {
  1445. MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
  1446. mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
  1447. }
  1448. static void
  1449. mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
  1450. {
  1451. sdev->no_uld_attach = data ? 1 : 0;
  1452. scsi_device_reprobe(sdev);
  1453. }
  1454. static void
  1455. mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
  1456. {
  1457. starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
  1458. mptsas_reprobe_lun);
  1459. }
  1460. /*
  1461. * Work queue thread to handle SAS hotplug events
  1462. */
  1463. static void
  1464. mptsas_hotplug_work(void *arg)
  1465. {
  1466. struct mptsas_hotplug_event *ev = arg;
  1467. MPT_ADAPTER *ioc = ev->ioc;
  1468. struct mptsas_phyinfo *phy_info;
  1469. struct sas_rphy *rphy;
  1470. struct scsi_device *sdev;
  1471. struct sas_identify identify;
  1472. char *ds = NULL;
  1473. struct mptsas_devinfo sas_device;
  1474. VirtTarget *vtarget;
  1475. mutex_lock(&ioc->sas_discovery_mutex);
  1476. switch (ev->event_type) {
  1477. case MPTSAS_DEL_DEVICE:
  1478. phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
  1479. /*
  1480. * Sanity checks, for non-existing phys and remote rphys.
  1481. */
  1482. if (!phy_info)
  1483. break;
  1484. if (!phy_info->rphy)
  1485. break;
  1486. if (phy_info->starget) {
  1487. vtarget = phy_info->starget->hostdata;
  1488. if (!vtarget)
  1489. break;
  1490. /*
  1491. * Handling RAID components
  1492. */
  1493. if (ev->phys_disk_num_valid) {
  1494. vtarget->target_id = ev->phys_disk_num;
  1495. vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  1496. mptsas_reprobe_target(vtarget->starget, 1);
  1497. break;
  1498. }
  1499. }
  1500. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1501. ds = "ssp";
  1502. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1503. ds = "stp";
  1504. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1505. ds = "sata";
  1506. printk(MYIOC_s_INFO_FMT
  1507. "removing %s device, channel %d, id %d, phy %d\n",
  1508. ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
  1509. sas_rphy_delete(phy_info->rphy);
  1510. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  1511. phy_info->rphy = NULL;
  1512. phy_info->starget = NULL;
  1513. break;
  1514. case MPTSAS_ADD_DEVICE:
  1515. /*
  1516. * Refresh sas device pg0 data
  1517. */
  1518. if (mptsas_sas_device_pg0(ioc, &sas_device,
  1519. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  1520. MPI_SAS_DEVICE_PGAD_FORM_SHIFT), ev->id))
  1521. break;
  1522. phy_info = mptsas_find_phyinfo_by_parent(ioc,
  1523. sas_device.handle_parent, sas_device.phy_id);
  1524. if (!phy_info) {
  1525. u32 handle = 0xFFFF;
  1526. /*
  1527. * Its possible when an expander has been hot added
  1528. * containing attached devices, the sas firmware
  1529. * may send a RC_ADDED event prior to the
  1530. * DISCOVERY STOP event. If that occurs, our
  1531. * view of the topology in the driver in respect to this
  1532. * expander might of not been setup, and we hit this
  1533. * condition.
  1534. * Therefore, this code kicks off discovery to
  1535. * refresh the data.
  1536. * Then again, we check whether the parent phy has
  1537. * been created.
  1538. */
  1539. ioc->sas_discovery_runtime=1;
  1540. mptsas_delete_expander_phys(ioc);
  1541. mptsas_probe_hba_phys(ioc);
  1542. while (!mptsas_probe_expander_phys(ioc, &handle))
  1543. ;
  1544. ioc->sas_discovery_runtime=0;
  1545. phy_info = mptsas_find_phyinfo_by_parent(ioc,
  1546. sas_device.handle_parent, sas_device.phy_id);
  1547. if (!phy_info)
  1548. break;
  1549. }
  1550. if (phy_info->starget) {
  1551. vtarget = phy_info->starget->hostdata;
  1552. if (!vtarget)
  1553. break;
  1554. /*
  1555. * Handling RAID components
  1556. */
  1557. if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1558. vtarget->tflags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  1559. vtarget->target_id = ev->id;
  1560. mptsas_reprobe_target(phy_info->starget, 0);
  1561. }
  1562. break;
  1563. }
  1564. if (phy_info->rphy)
  1565. break;
  1566. memcpy(&phy_info->attached, &sas_device,
  1567. sizeof(struct mptsas_devinfo));
  1568. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1569. ds = "ssp";
  1570. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1571. ds = "stp";
  1572. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1573. ds = "sata";
  1574. printk(MYIOC_s_INFO_FMT
  1575. "attaching %s device, channel %d, id %d, phy %d\n",
  1576. ioc->name, ds, ev->channel, ev->id, ev->phy_id);
  1577. mptsas_parse_device_info(&identify, &phy_info->attached);
  1578. switch (identify.device_type) {
  1579. case SAS_END_DEVICE:
  1580. rphy = sas_end_device_alloc(phy_info->phy);
  1581. break;
  1582. case SAS_EDGE_EXPANDER_DEVICE:
  1583. case SAS_FANOUT_EXPANDER_DEVICE:
  1584. rphy = sas_expander_alloc(phy_info->phy, identify.device_type);
  1585. break;
  1586. default:
  1587. rphy = NULL;
  1588. break;
  1589. }
  1590. if (!rphy)
  1591. break; /* non-fatal: an rphy can be added later */
  1592. rphy->identify = identify;
  1593. if (sas_rphy_add(rphy)) {
  1594. sas_rphy_free(rphy);
  1595. break;
  1596. }
  1597. phy_info->rphy = rphy;
  1598. break;
  1599. case MPTSAS_ADD_RAID:
  1600. sdev = scsi_device_lookup(
  1601. ioc->sh,
  1602. ioc->num_ports,
  1603. ev->id,
  1604. 0);
  1605. if (sdev) {
  1606. scsi_device_put(sdev);
  1607. break;
  1608. }
  1609. printk(MYIOC_s_INFO_FMT
  1610. "attaching raid volume, channel %d, id %d\n",
  1611. ioc->name, ioc->num_ports, ev->id);
  1612. scsi_add_device(ioc->sh,
  1613. ioc->num_ports,
  1614. ev->id,
  1615. 0);
  1616. mpt_findImVolumes(ioc);
  1617. break;
  1618. case MPTSAS_DEL_RAID:
  1619. sdev = scsi_device_lookup(
  1620. ioc->sh,
  1621. ioc->num_ports,
  1622. ev->id,
  1623. 0);
  1624. if (!sdev)
  1625. break;
  1626. printk(MYIOC_s_INFO_FMT
  1627. "removing raid volume, channel %d, id %d\n",
  1628. ioc->name, ioc->num_ports, ev->id);
  1629. scsi_remove_device(sdev);
  1630. scsi_device_put(sdev);
  1631. mpt_findImVolumes(ioc);
  1632. break;
  1633. }
  1634. kfree(ev);
  1635. mutex_unlock(&ioc->sas_discovery_mutex);
  1636. }
  1637. static void
  1638. mptscsih_send_sas_event(MPT_ADAPTER *ioc,
  1639. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  1640. {
  1641. struct mptsas_hotplug_event *ev;
  1642. u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  1643. __le64 sas_address;
  1644. if ((device_info &
  1645. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  1646. MPI_SAS_DEVICE_INFO_STP_TARGET |
  1647. MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
  1648. return;
  1649. switch (sas_event_data->ReasonCode) {
  1650. case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
  1651. case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
  1652. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  1653. if (!ev) {
  1654. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  1655. break;
  1656. }
  1657. INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
  1658. ev->ioc = ioc;
  1659. ev->handle = le16_to_cpu(sas_event_data->DevHandle);
  1660. ev->parent_handle =
  1661. le16_to_cpu(sas_event_data->ParentDevHandle);
  1662. ev->channel = sas_event_data->Bus;
  1663. ev->id = sas_event_data->TargetID;
  1664. ev->phy_id = sas_event_data->PhyNum;
  1665. memcpy(&sas_address, &sas_event_data->SASAddress,
  1666. sizeof(__le64));
  1667. ev->sas_address = le64_to_cpu(sas_address);
  1668. ev->device_info = device_info;
  1669. if (sas_event_data->ReasonCode &
  1670. MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  1671. ev->event_type = MPTSAS_ADD_DEVICE;
  1672. else
  1673. ev->event_type = MPTSAS_DEL_DEVICE;
  1674. schedule_work(&ev->work);
  1675. break;
  1676. case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
  1677. /*
  1678. * Persistent table is full.
  1679. */
  1680. INIT_WORK(&ioc->mptscsih_persistTask,
  1681. mptscsih_sas_persist_clear_table,
  1682. (void *)ioc);
  1683. schedule_work(&ioc->mptscsih_persistTask);
  1684. break;
  1685. case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  1686. /* TODO */
  1687. case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  1688. /* TODO */
  1689. default:
  1690. break;
  1691. }
  1692. }
  1693. static void
  1694. mptscsih_send_raid_event(MPT_ADAPTER *ioc,
  1695. EVENT_DATA_RAID *raid_event_data)
  1696. {
  1697. struct mptsas_hotplug_event *ev;
  1698. RAID_VOL0_STATUS * volumeStatus;
  1699. if (ioc->bus_type != SAS)
  1700. return;
  1701. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  1702. if (!ev) {
  1703. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  1704. return;
  1705. }
  1706. memset(ev,0,sizeof(struct mptsas_hotplug_event));
  1707. INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
  1708. ev->ioc = ioc;
  1709. ev->id = raid_event_data->VolumeID;
  1710. switch (raid_event_data->ReasonCode) {
  1711. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  1712. ev->event_type = MPTSAS_ADD_DEVICE;
  1713. break;
  1714. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  1715. ioc->raid_data.isRaid = 1;
  1716. ev->phys_disk_num_valid = 1;
  1717. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  1718. ev->event_type = MPTSAS_DEL_DEVICE;
  1719. break;
  1720. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  1721. ev->event_type = MPTSAS_DEL_RAID;
  1722. break;
  1723. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  1724. ev->event_type = MPTSAS_ADD_RAID;
  1725. break;
  1726. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  1727. volumeStatus = (RAID_VOL0_STATUS *) &
  1728. raid_event_data->SettingsStatus;
  1729. ev->event_type = (volumeStatus->State ==
  1730. MPI_RAIDVOL0_STATUS_STATE_FAILED) ?
  1731. MPTSAS_DEL_RAID : MPTSAS_ADD_RAID;
  1732. break;
  1733. default:
  1734. break;
  1735. }
  1736. schedule_work(&ev->work);
  1737. }
  1738. static void
  1739. mptscsih_send_discovery(MPT_ADAPTER *ioc,
  1740. EVENT_DATA_SAS_DISCOVERY *discovery_data)
  1741. {
  1742. struct mptsas_discovery_event *ev;
  1743. /*
  1744. * DiscoveryStatus
  1745. *
  1746. * This flag will be non-zero when firmware
  1747. * kicks off discovery, and return to zero
  1748. * once its completed.
  1749. */
  1750. if (discovery_data->DiscoveryStatus)
  1751. return;
  1752. ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
  1753. if (!ev)
  1754. return;
  1755. memset(ev,0,sizeof(struct mptsas_discovery_event));
  1756. INIT_WORK(&ev->work, mptscsih_discovery_work, ev);
  1757. ev->ioc = ioc;
  1758. schedule_work(&ev->work);
  1759. };
  1760. static int
  1761. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  1762. {
  1763. int rc=1;
  1764. u8 event = le32_to_cpu(reply->Event) & 0xFF;
  1765. if (!ioc->sh)
  1766. goto out;
  1767. /*
  1768. * sas_discovery_ignore_events
  1769. *
  1770. * This flag is to prevent anymore processing of
  1771. * sas events once mptsas_remove function is called.
  1772. */
  1773. if (ioc->sas_discovery_ignore_events) {
  1774. rc = mptscsih_event_process(ioc, reply);
  1775. goto out;
  1776. }
  1777. switch (event) {
  1778. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  1779. mptscsih_send_sas_event(ioc,
  1780. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
  1781. break;
  1782. case MPI_EVENT_INTEGRATED_RAID:
  1783. mptscsih_send_raid_event(ioc,
  1784. (EVENT_DATA_RAID *)reply->Data);
  1785. break;
  1786. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  1787. INIT_WORK(&ioc->mptscsih_persistTask,
  1788. mptscsih_sas_persist_clear_table,
  1789. (void *)ioc);
  1790. schedule_work(&ioc->mptscsih_persistTask);
  1791. break;
  1792. case MPI_EVENT_SAS_DISCOVERY:
  1793. mptscsih_send_discovery(ioc,
  1794. (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
  1795. break;
  1796. default:
  1797. rc = mptscsih_event_process(ioc, reply);
  1798. break;
  1799. }
  1800. out:
  1801. return rc;
  1802. }
  1803. static int
  1804. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1805. {
  1806. struct Scsi_Host *sh;
  1807. MPT_SCSI_HOST *hd;
  1808. MPT_ADAPTER *ioc;
  1809. unsigned long flags;
  1810. int ii;
  1811. int numSGE = 0;
  1812. int scale;
  1813. int ioc_cap;
  1814. int error=0;
  1815. int r;
  1816. r = mpt_attach(pdev,id);
  1817. if (r)
  1818. return r;
  1819. ioc = pci_get_drvdata(pdev);
  1820. ioc->DoneCtx = mptsasDoneCtx;
  1821. ioc->TaskCtx = mptsasTaskCtx;
  1822. ioc->InternalCtx = mptsasInternalCtx;
  1823. /* Added sanity check on readiness of the MPT adapter.
  1824. */
  1825. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  1826. printk(MYIOC_s_WARN_FMT
  1827. "Skipping because it's not operational!\n",
  1828. ioc->name);
  1829. error = -ENODEV;
  1830. goto out_mptsas_probe;
  1831. }
  1832. if (!ioc->active) {
  1833. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  1834. ioc->name);
  1835. error = -ENODEV;
  1836. goto out_mptsas_probe;
  1837. }
  1838. /* Sanity check - ensure at least 1 port is INITIATOR capable
  1839. */
  1840. ioc_cap = 0;
  1841. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  1842. if (ioc->pfacts[ii].ProtocolFlags &
  1843. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  1844. ioc_cap++;
  1845. }
  1846. if (!ioc_cap) {
  1847. printk(MYIOC_s_WARN_FMT
  1848. "Skipping ioc=%p because SCSI Initiator mode "
  1849. "is NOT enabled!\n", ioc->name, ioc);
  1850. return 0;
  1851. }
  1852. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  1853. if (!sh) {
  1854. printk(MYIOC_s_WARN_FMT
  1855. "Unable to register controller with SCSI subsystem\n",
  1856. ioc->name);
  1857. error = -1;
  1858. goto out_mptsas_probe;
  1859. }
  1860. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1861. /* Attach the SCSI Host to the IOC structure
  1862. */
  1863. ioc->sh = sh;
  1864. sh->io_port = 0;
  1865. sh->n_io_port = 0;
  1866. sh->irq = 0;
  1867. /* set 16 byte cdb's */
  1868. sh->max_cmd_len = 16;
  1869. sh->max_id = ioc->pfacts->MaxDevices + 1;
  1870. sh->transportt = mptsas_transport_template;
  1871. sh->max_lun = MPT_LAST_LUN + 1;
  1872. sh->max_channel = 0;
  1873. sh->this_id = ioc->pfacts[0].PortSCSIID;
  1874. /* Required entry.
  1875. */
  1876. sh->unique_id = ioc->id;
  1877. INIT_LIST_HEAD(&ioc->sas_topology);
  1878. mutex_init(&ioc->sas_topology_mutex);
  1879. mutex_init(&ioc->sas_discovery_mutex);
  1880. mutex_init(&ioc->sas_mgmt.mutex);
  1881. init_completion(&ioc->sas_mgmt.done);
  1882. /* Verify that we won't exceed the maximum
  1883. * number of chain buffers
  1884. * We can optimize: ZZ = req_sz/sizeof(SGE)
  1885. * For 32bit SGE's:
  1886. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  1887. * + (req_sz - 64)/sizeof(SGE)
  1888. * A slightly different algorithm is required for
  1889. * 64bit SGEs.
  1890. */
  1891. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  1892. if (sizeof(dma_addr_t) == sizeof(u64)) {
  1893. numSGE = (scale - 1) *
  1894. (ioc->facts.MaxChainDepth-1) + scale +
  1895. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  1896. sizeof(u32));
  1897. } else {
  1898. numSGE = 1 + (scale - 1) *
  1899. (ioc->facts.MaxChainDepth-1) + scale +
  1900. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  1901. sizeof(u32));
  1902. }
  1903. if (numSGE < sh->sg_tablesize) {
  1904. /* Reset this value */
  1905. dprintk((MYIOC_s_INFO_FMT
  1906. "Resetting sg_tablesize to %d from %d\n",
  1907. ioc->name, numSGE, sh->sg_tablesize));
  1908. sh->sg_tablesize = numSGE;
  1909. }
  1910. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1911. hd = (MPT_SCSI_HOST *) sh->hostdata;
  1912. hd->ioc = ioc;
  1913. /* SCSI needs scsi_cmnd lookup table!
  1914. * (with size equal to req_depth*PtrSz!)
  1915. */
  1916. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  1917. if (!hd->ScsiLookup) {
  1918. error = -ENOMEM;
  1919. goto out_mptsas_probe;
  1920. }
  1921. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  1922. ioc->name, hd->ScsiLookup));
  1923. /* Allocate memory for the device structures.
  1924. * A non-Null pointer at an offset
  1925. * indicates a device exists.
  1926. * max_id = 1 + maximum id (hosts.h)
  1927. */
  1928. hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
  1929. if (!hd->Targets) {
  1930. error = -ENOMEM;
  1931. goto out_mptsas_probe;
  1932. }
  1933. dprintk((KERN_INFO " vtarget @ %p\n", hd->Targets));
  1934. /* Clear the TM flags
  1935. */
  1936. hd->tmPending = 0;
  1937. hd->tmState = TM_STATE_NONE;
  1938. hd->resetPending = 0;
  1939. hd->abortSCpnt = NULL;
  1940. /* Clear the pointer used to store
  1941. * single-threaded commands, i.e., those
  1942. * issued during a bus scan, dv and
  1943. * configuration pages.
  1944. */
  1945. hd->cmdPtr = NULL;
  1946. /* Initialize this SCSI Hosts' timers
  1947. * To use, set the timer expires field
  1948. * and add_timer
  1949. */
  1950. init_timer(&hd->timer);
  1951. hd->timer.data = (unsigned long) hd;
  1952. hd->timer.function = mptscsih_timer_expired;
  1953. hd->mpt_pq_filter = mpt_pq_filter;
  1954. ioc->sas_data.ptClear = mpt_pt_clear;
  1955. if (ioc->sas_data.ptClear==1) {
  1956. mptbase_sas_persist_operation(
  1957. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  1958. }
  1959. ddvprintk((MYIOC_s_INFO_FMT
  1960. "mpt_pq_filter %x mpt_pq_filter %x\n",
  1961. ioc->name,
  1962. mpt_pq_filter,
  1963. mpt_pq_filter));
  1964. init_waitqueue_head(&hd->scandv_waitq);
  1965. hd->scandv_wait_done = 0;
  1966. hd->last_queue_full = 0;
  1967. error = scsi_add_host(sh, &ioc->pcidev->dev);
  1968. if (error) {
  1969. dprintk((KERN_ERR MYNAM
  1970. "scsi_add_host failed\n"));
  1971. goto out_mptsas_probe;
  1972. }
  1973. mptsas_scan_sas_topology(ioc);
  1974. return 0;
  1975. out_mptsas_probe:
  1976. mptscsih_remove(pdev);
  1977. return error;
  1978. }
  1979. static void __devexit mptsas_remove(struct pci_dev *pdev)
  1980. {
  1981. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1982. struct mptsas_portinfo *p, *n;
  1983. ioc->sas_discovery_ignore_events=1;
  1984. sas_remove_host(ioc->sh);
  1985. mutex_lock(&ioc->sas_topology_mutex);
  1986. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  1987. list_del(&p->list);
  1988. if (p->phy_info)
  1989. kfree(p->phy_info);
  1990. kfree(p);
  1991. }
  1992. mutex_unlock(&ioc->sas_topology_mutex);
  1993. mptscsih_remove(pdev);
  1994. }
  1995. static struct pci_device_id mptsas_pci_table[] = {
  1996. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064,
  1997. PCI_ANY_ID, PCI_ANY_ID },
  1998. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066,
  1999. PCI_ANY_ID, PCI_ANY_ID },
  2000. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068,
  2001. PCI_ANY_ID, PCI_ANY_ID },
  2002. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064E,
  2003. PCI_ANY_ID, PCI_ANY_ID },
  2004. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066E,
  2005. PCI_ANY_ID, PCI_ANY_ID },
  2006. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068E,
  2007. PCI_ANY_ID, PCI_ANY_ID },
  2008. {0} /* Terminating entry */
  2009. };
  2010. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  2011. static struct pci_driver mptsas_driver = {
  2012. .name = "mptsas",
  2013. .id_table = mptsas_pci_table,
  2014. .probe = mptsas_probe,
  2015. .remove = __devexit_p(mptsas_remove),
  2016. .shutdown = mptscsih_shutdown,
  2017. #ifdef CONFIG_PM
  2018. .suspend = mptscsih_suspend,
  2019. .resume = mptscsih_resume,
  2020. #endif
  2021. };
  2022. static int __init
  2023. mptsas_init(void)
  2024. {
  2025. show_mptmod_ver(my_NAME, my_VERSION);
  2026. mptsas_transport_template =
  2027. sas_attach_transport(&mptsas_transport_functions);
  2028. if (!mptsas_transport_template)
  2029. return -ENODEV;
  2030. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
  2031. mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
  2032. mptsasInternalCtx =
  2033. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
  2034. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
  2035. if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
  2036. devtverboseprintk((KERN_INFO MYNAM
  2037. ": Registered for IOC event notifications\n"));
  2038. }
  2039. if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
  2040. dprintk((KERN_INFO MYNAM
  2041. ": Registered for IOC reset notifications\n"));
  2042. }
  2043. return pci_register_driver(&mptsas_driver);
  2044. }
  2045. static void __exit
  2046. mptsas_exit(void)
  2047. {
  2048. pci_unregister_driver(&mptsas_driver);
  2049. sas_release_transport(mptsas_transport_template);
  2050. mpt_reset_deregister(mptsasDoneCtx);
  2051. mpt_event_deregister(mptsasDoneCtx);
  2052. mpt_deregister(mptsasMgmtCtx);
  2053. mpt_deregister(mptsasInternalCtx);
  2054. mpt_deregister(mptsasTaskCtx);
  2055. mpt_deregister(mptsasDoneCtx);
  2056. }
  2057. module_init(mptsas_init);
  2058. module_exit(mptsas_exit);