mptsas.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377
  1. /*
  2. * linux/drivers/message/fusion/mptsas.c
  3. * For use with LSI PCI chip/adapter(s)
  4. * running LSI Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2007 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. * Copyright (c) 2005-2007 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/jiffies.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_transport_sas.h>
  54. #include <scsi/scsi_dbg.h>
  55. #include "mptbase.h"
  56. #include "mptscsih.h"
  57. #include "mptsas.h"
  58. #define my_NAME "Fusion MPT SAS Host driver"
  59. #define my_VERSION MPT_LINUX_VERSION_COMMON
  60. #define MYNAM "mptsas"
  61. /*
  62. * Reserved channel for integrated raid
  63. */
  64. #define MPTSAS_RAID_CHANNEL 1
  65. MODULE_AUTHOR(MODULEAUTHOR);
  66. MODULE_DESCRIPTION(my_NAME);
  67. MODULE_LICENSE("GPL");
  68. MODULE_VERSION(my_VERSION);
  69. static int mpt_pt_clear;
  70. module_param(mpt_pt_clear, int, 0);
  71. MODULE_PARM_DESC(mpt_pt_clear,
  72. " Clear persistency table: enable=1 "
  73. "(default=MPTSCSIH_PT_CLEAR=0)");
  74. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  75. #define MPTSAS_MAX_LUN (16895)
  76. static int max_lun = MPTSAS_MAX_LUN;
  77. module_param(max_lun, int, 0);
  78. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  79. static u8 mptsasDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
  80. static u8 mptsasTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
  81. static u8 mptsasInternalCtx = MPT_MAX_PROTOCOL_DRIVERS; /* Used only for internal commands */
  82. static u8 mptsasMgmtCtx = MPT_MAX_PROTOCOL_DRIVERS;
  83. static void mptsas_hotplug_work(struct work_struct *work);
  84. static void mptsas_print_phy_data(MPT_ADAPTER *ioc,
  85. MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
  86. {
  87. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  88. "---- IO UNIT PAGE 0 ------------\n", ioc->name));
  89. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
  90. ioc->name, le16_to_cpu(phy_data->AttachedDeviceHandle)));
  91. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Controller Handle=0x%X\n",
  92. ioc->name, le16_to_cpu(phy_data->ControllerDevHandle)));
  93. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port=0x%X\n",
  94. ioc->name, phy_data->Port));
  95. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port Flags=0x%X\n",
  96. ioc->name, phy_data->PortFlags));
  97. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Flags=0x%X\n",
  98. ioc->name, phy_data->PhyFlags));
  99. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
  100. ioc->name, phy_data->NegotiatedLinkRate));
  101. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  102. "Controller PHY Device Info=0x%X\n", ioc->name,
  103. le32_to_cpu(phy_data->ControllerPhyDeviceInfo)));
  104. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "DiscoveryStatus=0x%X\n\n",
  105. ioc->name, le32_to_cpu(phy_data->DiscoveryStatus)));
  106. }
  107. static void mptsas_print_phy_pg0(MPT_ADAPTER *ioc, SasPhyPage0_t *pg0)
  108. {
  109. __le64 sas_address;
  110. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  111. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  112. "---- SAS PHY PAGE 0 ------------\n", ioc->name));
  113. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  114. "Attached Device Handle=0x%X\n", ioc->name,
  115. le16_to_cpu(pg0->AttachedDevHandle)));
  116. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
  117. ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
  118. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  119. "Attached PHY Identifier=0x%X\n", ioc->name,
  120. pg0->AttachedPhyIdentifier));
  121. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Attached Device Info=0x%X\n",
  122. ioc->name, le32_to_cpu(pg0->AttachedDeviceInfo)));
  123. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
  124. ioc->name, pg0->ProgrammedLinkRate));
  125. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Change Count=0x%X\n",
  126. ioc->name, pg0->ChangeCount));
  127. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Info=0x%X\n\n",
  128. ioc->name, le32_to_cpu(pg0->PhyInfo)));
  129. }
  130. static void mptsas_print_phy_pg1(MPT_ADAPTER *ioc, SasPhyPage1_t *pg1)
  131. {
  132. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  133. "---- SAS PHY PAGE 1 ------------\n", ioc->name));
  134. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Invalid Dword Count=0x%x\n",
  135. ioc->name, pg1->InvalidDwordCount));
  136. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  137. "Running Disparity Error Count=0x%x\n", ioc->name,
  138. pg1->RunningDisparityErrorCount));
  139. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  140. "Loss Dword Synch Count=0x%x\n", ioc->name,
  141. pg1->LossDwordSynchCount));
  142. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  143. "PHY Reset Problem Count=0x%x\n\n", ioc->name,
  144. pg1->PhyResetProblemCount));
  145. }
  146. static void mptsas_print_device_pg0(MPT_ADAPTER *ioc, SasDevicePage0_t *pg0)
  147. {
  148. __le64 sas_address;
  149. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  150. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  151. "---- SAS DEVICE PAGE 0 ---------\n", ioc->name));
  152. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
  153. ioc->name, le16_to_cpu(pg0->DevHandle)));
  154. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Handle=0x%X\n",
  155. ioc->name, le16_to_cpu(pg0->ParentDevHandle)));
  156. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Enclosure Handle=0x%X\n",
  157. ioc->name, le16_to_cpu(pg0->EnclosureHandle)));
  158. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Slot=0x%X\n",
  159. ioc->name, le16_to_cpu(pg0->Slot)));
  160. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
  161. ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
  162. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Target ID=0x%X\n",
  163. ioc->name, pg0->TargetID));
  164. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Bus=0x%X\n",
  165. ioc->name, pg0->Bus));
  166. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Phy Num=0x%X\n",
  167. ioc->name, pg0->PhyNum));
  168. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Access Status=0x%X\n",
  169. ioc->name, le16_to_cpu(pg0->AccessStatus)));
  170. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Device Info=0x%X\n",
  171. ioc->name, le32_to_cpu(pg0->DeviceInfo)));
  172. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Flags=0x%X\n",
  173. ioc->name, le16_to_cpu(pg0->Flags)));
  174. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n\n",
  175. ioc->name, pg0->PhysicalPort));
  176. }
  177. static void mptsas_print_expander_pg1(MPT_ADAPTER *ioc, SasExpanderPage1_t *pg1)
  178. {
  179. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  180. "---- SAS EXPANDER PAGE 1 ------------\n", ioc->name));
  181. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n",
  182. ioc->name, pg1->PhysicalPort));
  183. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Identifier=0x%X\n",
  184. ioc->name, pg1->PhyIdentifier));
  185. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
  186. ioc->name, pg1->NegotiatedLinkRate));
  187. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
  188. ioc->name, pg1->ProgrammedLinkRate));
  189. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hardware Link Rate=0x%X\n",
  190. ioc->name, pg1->HwLinkRate));
  191. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Owner Device Handle=0x%X\n",
  192. ioc->name, le16_to_cpu(pg1->OwnerDevHandle)));
  193. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  194. "Attached Device Handle=0x%X\n\n", ioc->name,
  195. le16_to_cpu(pg1->AttachedDevHandle)));
  196. }
  197. static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
  198. {
  199. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  200. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  201. }
  202. static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
  203. {
  204. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  205. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  206. }
  207. /*
  208. * mptsas_find_portinfo_by_handle
  209. *
  210. * This function should be called with the sas_topology_mutex already held
  211. */
  212. static struct mptsas_portinfo *
  213. mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
  214. {
  215. struct mptsas_portinfo *port_info, *rc=NULL;
  216. int i;
  217. list_for_each_entry(port_info, &ioc->sas_topology, list)
  218. for (i = 0; i < port_info->num_phys; i++)
  219. if (port_info->phy_info[i].identify.handle == handle) {
  220. rc = port_info;
  221. goto out;
  222. }
  223. out:
  224. return rc;
  225. }
  226. /*
  227. * Returns true if there is a scsi end device
  228. */
  229. static inline int
  230. mptsas_is_end_device(struct mptsas_devinfo * attached)
  231. {
  232. if ((attached->sas_address) &&
  233. (attached->device_info &
  234. MPI_SAS_DEVICE_INFO_END_DEVICE) &&
  235. ((attached->device_info &
  236. MPI_SAS_DEVICE_INFO_SSP_TARGET) |
  237. (attached->device_info &
  238. MPI_SAS_DEVICE_INFO_STP_TARGET) |
  239. (attached->device_info &
  240. MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
  241. return 1;
  242. else
  243. return 0;
  244. }
  245. /* no mutex */
  246. static void
  247. mptsas_port_delete(MPT_ADAPTER *ioc, struct mptsas_portinfo_details * port_details)
  248. {
  249. struct mptsas_portinfo *port_info;
  250. struct mptsas_phyinfo *phy_info;
  251. u8 i;
  252. if (!port_details)
  253. return;
  254. port_info = port_details->port_info;
  255. phy_info = port_info->phy_info;
  256. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: [%p]: num_phys=%02d "
  257. "bitmask=0x%016llX\n", ioc->name, __FUNCTION__, port_details,
  258. port_details->num_phys, (unsigned long long)
  259. port_details->phy_bitmask));
  260. for (i = 0; i < port_info->num_phys; i++, phy_info++) {
  261. if(phy_info->port_details != port_details)
  262. continue;
  263. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  264. phy_info->port_details = NULL;
  265. }
  266. kfree(port_details);
  267. }
  268. static inline struct sas_rphy *
  269. mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
  270. {
  271. if (phy_info->port_details)
  272. return phy_info->port_details->rphy;
  273. else
  274. return NULL;
  275. }
  276. static inline void
  277. mptsas_set_rphy(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
  278. {
  279. if (phy_info->port_details) {
  280. phy_info->port_details->rphy = rphy;
  281. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_rphy_add: rphy=%p\n",
  282. ioc->name, rphy));
  283. }
  284. if (rphy) {
  285. dsaswideprintk(ioc, dev_printk(MYIOC_s_DEBUG_FMT,
  286. &rphy->dev, "add:", ioc->name));
  287. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "rphy=%p release=%p\n",
  288. ioc->name, rphy, rphy->dev.release));
  289. }
  290. }
  291. static inline struct sas_port *
  292. mptsas_get_port(struct mptsas_phyinfo *phy_info)
  293. {
  294. if (phy_info->port_details)
  295. return phy_info->port_details->port;
  296. else
  297. return NULL;
  298. }
  299. static inline void
  300. mptsas_set_port(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_port *port)
  301. {
  302. if (phy_info->port_details)
  303. phy_info->port_details->port = port;
  304. if (port) {
  305. dsaswideprintk(ioc, dev_printk(MYIOC_s_DEBUG_FMT,
  306. &port->dev, "add:", ioc->name));
  307. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "port=%p release=%p\n",
  308. ioc->name, port, port->dev.release));
  309. }
  310. }
  311. static inline struct scsi_target *
  312. mptsas_get_starget(struct mptsas_phyinfo *phy_info)
  313. {
  314. if (phy_info->port_details)
  315. return phy_info->port_details->starget;
  316. else
  317. return NULL;
  318. }
  319. static inline void
  320. mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
  321. starget)
  322. {
  323. if (phy_info->port_details)
  324. phy_info->port_details->starget = starget;
  325. }
  326. /*
  327. * mptsas_setup_wide_ports
  328. *
  329. * Updates for new and existing narrow/wide port configuration
  330. * in the sas_topology
  331. */
  332. static void
  333. mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  334. {
  335. struct mptsas_portinfo_details * port_details;
  336. struct mptsas_phyinfo *phy_info, *phy_info_cmp;
  337. u64 sas_address;
  338. int i, j;
  339. mutex_lock(&ioc->sas_topology_mutex);
  340. phy_info = port_info->phy_info;
  341. for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
  342. if (phy_info->attached.handle)
  343. continue;
  344. port_details = phy_info->port_details;
  345. if (!port_details)
  346. continue;
  347. if (port_details->num_phys < 2)
  348. continue;
  349. /*
  350. * Removing a phy from a port, letting the last
  351. * phy be removed by firmware events.
  352. */
  353. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  354. "%s: [%p]: deleting phy = %d\n",
  355. ioc->name, __FUNCTION__, port_details, i));
  356. port_details->num_phys--;
  357. port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
  358. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  359. sas_port_delete_phy(port_details->port, phy_info->phy);
  360. phy_info->port_details = NULL;
  361. }
  362. /*
  363. * Populate and refresh the tree
  364. */
  365. phy_info = port_info->phy_info;
  366. for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
  367. sas_address = phy_info->attached.sas_address;
  368. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "phy_id=%d sas_address=0x%018llX\n",
  369. ioc->name, i, (unsigned long long)sas_address));
  370. if (!sas_address)
  371. continue;
  372. port_details = phy_info->port_details;
  373. /*
  374. * Forming a port
  375. */
  376. if (!port_details) {
  377. port_details = kzalloc(sizeof(*port_details),
  378. GFP_KERNEL);
  379. if (!port_details)
  380. goto out;
  381. port_details->num_phys = 1;
  382. port_details->port_info = port_info;
  383. if (phy_info->phy_id < 64 )
  384. port_details->phy_bitmask |=
  385. (1 << phy_info->phy_id);
  386. phy_info->sas_port_add_phy=1;
  387. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tForming port\n\t\t"
  388. "phy_id=%d sas_address=0x%018llX\n",
  389. ioc->name, i, (unsigned long long)sas_address));
  390. phy_info->port_details = port_details;
  391. }
  392. if (i == port_info->num_phys - 1)
  393. continue;
  394. phy_info_cmp = &port_info->phy_info[i + 1];
  395. for (j = i + 1 ; j < port_info->num_phys ; j++,
  396. phy_info_cmp++) {
  397. if (!phy_info_cmp->attached.sas_address)
  398. continue;
  399. if (sas_address != phy_info_cmp->attached.sas_address)
  400. continue;
  401. if (phy_info_cmp->port_details == port_details )
  402. continue;
  403. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  404. "\t\tphy_id=%d sas_address=0x%018llX\n",
  405. ioc->name, j, (unsigned long long)
  406. phy_info_cmp->attached.sas_address));
  407. if (phy_info_cmp->port_details) {
  408. port_details->rphy =
  409. mptsas_get_rphy(phy_info_cmp);
  410. port_details->port =
  411. mptsas_get_port(phy_info_cmp);
  412. port_details->starget =
  413. mptsas_get_starget(phy_info_cmp);
  414. port_details->num_phys =
  415. phy_info_cmp->port_details->num_phys;
  416. if (!phy_info_cmp->port_details->num_phys)
  417. kfree(phy_info_cmp->port_details);
  418. } else
  419. phy_info_cmp->sas_port_add_phy=1;
  420. /*
  421. * Adding a phy to a port
  422. */
  423. phy_info_cmp->port_details = port_details;
  424. if (phy_info_cmp->phy_id < 64 )
  425. port_details->phy_bitmask |=
  426. (1 << phy_info_cmp->phy_id);
  427. port_details->num_phys++;
  428. }
  429. }
  430. out:
  431. for (i = 0; i < port_info->num_phys; i++) {
  432. port_details = port_info->phy_info[i].port_details;
  433. if (!port_details)
  434. continue;
  435. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  436. "%s: [%p]: phy_id=%02d num_phys=%02d "
  437. "bitmask=0x%016llX\n", ioc->name, __FUNCTION__,
  438. port_details, i, port_details->num_phys,
  439. (unsigned long long)port_details->phy_bitmask));
  440. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tport = %p rphy=%p\n",
  441. ioc->name, port_details->port, port_details->rphy));
  442. }
  443. dsaswideprintk(ioc, printk("\n"));
  444. mutex_unlock(&ioc->sas_topology_mutex);
  445. }
  446. /**
  447. * csmisas_find_vtarget
  448. *
  449. * @ioc
  450. * @volume_id
  451. * @volume_bus
  452. *
  453. **/
  454. static VirtTarget *
  455. mptsas_find_vtarget(MPT_ADAPTER *ioc, u8 channel, u8 id)
  456. {
  457. struct scsi_device *sdev;
  458. VirtDevice *vdevice;
  459. VirtTarget *vtarget = NULL;
  460. shost_for_each_device(sdev, ioc->sh) {
  461. if ((vdevice = sdev->hostdata) == NULL)
  462. continue;
  463. if (vdevice->vtarget->id == id &&
  464. vdevice->vtarget->channel == channel)
  465. vtarget = vdevice->vtarget;
  466. }
  467. return vtarget;
  468. }
  469. /**
  470. * mptsas_target_reset
  471. *
  472. * Issues TARGET_RESET to end device using handshaking method
  473. *
  474. * @ioc
  475. * @channel
  476. * @id
  477. *
  478. * Returns (1) success
  479. * (0) failure
  480. *
  481. **/
  482. static int
  483. mptsas_target_reset(MPT_ADAPTER *ioc, u8 channel, u8 id)
  484. {
  485. MPT_FRAME_HDR *mf;
  486. SCSITaskMgmt_t *pScsiTm;
  487. if ((mf = mpt_get_msg_frame(ioc->TaskCtx, ioc)) == NULL) {
  488. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, no msg frames @%d!!\n",
  489. ioc->name,__FUNCTION__, __LINE__));
  490. return 0;
  491. }
  492. /* Format the Request
  493. */
  494. pScsiTm = (SCSITaskMgmt_t *) mf;
  495. memset (pScsiTm, 0, sizeof(SCSITaskMgmt_t));
  496. pScsiTm->TargetID = id;
  497. pScsiTm->Bus = channel;
  498. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  499. pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  500. pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
  501. DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
  502. mpt_put_msg_frame_hi_pri(ioc->TaskCtx, ioc, mf);
  503. return 1;
  504. }
  505. /**
  506. * mptsas_target_reset_queue
  507. *
  508. * Receive request for TARGET_RESET after recieving an firmware
  509. * event NOT_RESPONDING_EVENT, then put command in link list
  510. * and queue if task_queue already in use.
  511. *
  512. * @ioc
  513. * @sas_event_data
  514. *
  515. **/
  516. static void
  517. mptsas_target_reset_queue(MPT_ADAPTER *ioc,
  518. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  519. {
  520. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
  521. VirtTarget *vtarget = NULL;
  522. struct mptsas_target_reset_event *target_reset_list;
  523. u8 id, channel;
  524. id = sas_event_data->TargetID;
  525. channel = sas_event_data->Bus;
  526. if (!(vtarget = mptsas_find_vtarget(ioc, channel, id)))
  527. return;
  528. vtarget->deleted = 1; /* block IO */
  529. target_reset_list = kzalloc(sizeof(*target_reset_list),
  530. GFP_ATOMIC);
  531. if (!target_reset_list) {
  532. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
  533. ioc->name,__FUNCTION__, __LINE__));
  534. return;
  535. }
  536. memcpy(&target_reset_list->sas_event_data, sas_event_data,
  537. sizeof(*sas_event_data));
  538. list_add_tail(&target_reset_list->list, &hd->target_reset_list);
  539. if (hd->resetPending)
  540. return;
  541. if (mptsas_target_reset(ioc, channel, id)) {
  542. target_reset_list->target_reset_issued = 1;
  543. hd->resetPending = 1;
  544. }
  545. }
  546. /**
  547. * mptsas_dev_reset_complete
  548. *
  549. * Completion for TARGET_RESET after NOT_RESPONDING_EVENT,
  550. * enable work queue to finish off removing device from upper layers.
  551. * then send next TARGET_RESET in the queue.
  552. *
  553. * @ioc
  554. *
  555. **/
  556. static void
  557. mptsas_dev_reset_complete(MPT_ADAPTER *ioc)
  558. {
  559. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
  560. struct list_head *head = &hd->target_reset_list;
  561. struct mptsas_target_reset_event *target_reset_list;
  562. struct mptsas_hotplug_event *ev;
  563. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data;
  564. u8 id, channel;
  565. __le64 sas_address;
  566. if (list_empty(head))
  567. return;
  568. target_reset_list = list_entry(head->next, struct mptsas_target_reset_event, list);
  569. sas_event_data = &target_reset_list->sas_event_data;
  570. id = sas_event_data->TargetID;
  571. channel = sas_event_data->Bus;
  572. hd->resetPending = 0;
  573. /*
  574. * retry target reset
  575. */
  576. if (!target_reset_list->target_reset_issued) {
  577. if (mptsas_target_reset(ioc, channel, id)) {
  578. target_reset_list->target_reset_issued = 1;
  579. hd->resetPending = 1;
  580. }
  581. return;
  582. }
  583. /*
  584. * enable work queue to remove device from upper layers
  585. */
  586. list_del(&target_reset_list->list);
  587. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  588. if (!ev) {
  589. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
  590. ioc->name,__FUNCTION__, __LINE__));
  591. return;
  592. }
  593. INIT_WORK(&ev->work, mptsas_hotplug_work);
  594. ev->ioc = ioc;
  595. ev->handle = le16_to_cpu(sas_event_data->DevHandle);
  596. ev->parent_handle =
  597. le16_to_cpu(sas_event_data->ParentDevHandle);
  598. ev->channel = channel;
  599. ev->id =id;
  600. ev->phy_id = sas_event_data->PhyNum;
  601. memcpy(&sas_address, &sas_event_data->SASAddress,
  602. sizeof(__le64));
  603. ev->sas_address = le64_to_cpu(sas_address);
  604. ev->device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  605. ev->event_type = MPTSAS_DEL_DEVICE;
  606. schedule_work(&ev->work);
  607. kfree(target_reset_list);
  608. /*
  609. * issue target reset to next device in the queue
  610. */
  611. head = &hd->target_reset_list;
  612. if (list_empty(head))
  613. return;
  614. target_reset_list = list_entry(head->next, struct mptsas_target_reset_event,
  615. list);
  616. sas_event_data = &target_reset_list->sas_event_data;
  617. id = sas_event_data->TargetID;
  618. channel = sas_event_data->Bus;
  619. if (mptsas_target_reset(ioc, channel, id)) {
  620. target_reset_list->target_reset_issued = 1;
  621. hd->resetPending = 1;
  622. }
  623. }
  624. /**
  625. * mptsas_taskmgmt_complete
  626. *
  627. * @ioc
  628. * @mf
  629. * @mr
  630. *
  631. **/
  632. static int
  633. mptsas_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  634. {
  635. mptsas_dev_reset_complete(ioc);
  636. return mptscsih_taskmgmt_complete(ioc, mf, mr);
  637. }
  638. /**
  639. * mptscsih_ioc_reset
  640. *
  641. * @ioc
  642. * @reset_phase
  643. *
  644. **/
  645. static int
  646. mptsas_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  647. {
  648. MPT_SCSI_HOST *hd;
  649. struct mptsas_target_reset_event *target_reset_list, *n;
  650. int rc;
  651. rc = mptscsih_ioc_reset(ioc, reset_phase);
  652. if (ioc->bus_type != SAS)
  653. goto out;
  654. if (reset_phase != MPT_IOC_POST_RESET)
  655. goto out;
  656. if (!ioc->sh || !ioc->sh->hostdata)
  657. goto out;
  658. hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
  659. if (!hd->ioc)
  660. goto out;
  661. if (list_empty(&hd->target_reset_list))
  662. goto out;
  663. /* flush the target_reset_list */
  664. list_for_each_entry_safe(target_reset_list, n,
  665. &hd->target_reset_list, list) {
  666. list_del(&target_reset_list->list);
  667. kfree(target_reset_list);
  668. }
  669. out:
  670. return rc;
  671. }
  672. static int
  673. mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
  674. u32 form, u32 form_specific)
  675. {
  676. ConfigExtendedPageHeader_t hdr;
  677. CONFIGPARMS cfg;
  678. SasEnclosurePage0_t *buffer;
  679. dma_addr_t dma_handle;
  680. int error;
  681. __le64 le_identifier;
  682. memset(&hdr, 0, sizeof(hdr));
  683. hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
  684. hdr.PageNumber = 0;
  685. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  686. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
  687. cfg.cfghdr.ehdr = &hdr;
  688. cfg.physAddr = -1;
  689. cfg.pageAddr = form + form_specific;
  690. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  691. cfg.dir = 0; /* read */
  692. cfg.timeout = 10;
  693. error = mpt_config(ioc, &cfg);
  694. if (error)
  695. goto out;
  696. if (!hdr.ExtPageLength) {
  697. error = -ENXIO;
  698. goto out;
  699. }
  700. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  701. &dma_handle);
  702. if (!buffer) {
  703. error = -ENOMEM;
  704. goto out;
  705. }
  706. cfg.physAddr = dma_handle;
  707. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  708. error = mpt_config(ioc, &cfg);
  709. if (error)
  710. goto out_free_consistent;
  711. /* save config data */
  712. memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
  713. enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
  714. enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
  715. enclosure->flags = le16_to_cpu(buffer->Flags);
  716. enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
  717. enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
  718. enclosure->start_id = buffer->StartTargetID;
  719. enclosure->start_channel = buffer->StartBus;
  720. enclosure->sep_id = buffer->SEPTargetID;
  721. enclosure->sep_channel = buffer->SEPBus;
  722. out_free_consistent:
  723. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  724. buffer, dma_handle);
  725. out:
  726. return error;
  727. }
  728. static int
  729. mptsas_slave_configure(struct scsi_device *sdev)
  730. {
  731. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  732. goto out;
  733. sas_read_port_mode_page(sdev);
  734. out:
  735. return mptscsih_slave_configure(sdev);
  736. }
  737. static int
  738. mptsas_target_alloc(struct scsi_target *starget)
  739. {
  740. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  741. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  742. VirtTarget *vtarget;
  743. u8 id, channel;
  744. struct sas_rphy *rphy;
  745. struct mptsas_portinfo *p;
  746. int i;
  747. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  748. if (!vtarget)
  749. return -ENOMEM;
  750. vtarget->starget = starget;
  751. vtarget->ioc_id = hd->ioc->id;
  752. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  753. id = starget->id;
  754. channel = 0;
  755. /*
  756. * RAID volumes placed beyond the last expected port.
  757. */
  758. if (starget->channel == MPTSAS_RAID_CHANNEL) {
  759. for (i=0; i < hd->ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
  760. if (id == hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID)
  761. channel = hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeBus;
  762. goto out;
  763. }
  764. rphy = dev_to_rphy(starget->dev.parent);
  765. mutex_lock(&hd->ioc->sas_topology_mutex);
  766. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  767. for (i = 0; i < p->num_phys; i++) {
  768. if (p->phy_info[i].attached.sas_address !=
  769. rphy->identify.sas_address)
  770. continue;
  771. id = p->phy_info[i].attached.id;
  772. channel = p->phy_info[i].attached.channel;
  773. mptsas_set_starget(&p->phy_info[i], starget);
  774. /*
  775. * Exposing hidden raid components
  776. */
  777. if (mptscsih_is_phys_disk(hd->ioc, channel, id)) {
  778. id = mptscsih_raid_id_to_num(hd->ioc,
  779. channel, id);
  780. vtarget->tflags |=
  781. MPT_TARGET_FLAGS_RAID_COMPONENT;
  782. p->phy_info[i].attached.phys_disk_num = id;
  783. }
  784. mutex_unlock(&hd->ioc->sas_topology_mutex);
  785. goto out;
  786. }
  787. }
  788. mutex_unlock(&hd->ioc->sas_topology_mutex);
  789. kfree(vtarget);
  790. return -ENXIO;
  791. out:
  792. vtarget->id = id;
  793. vtarget->channel = channel;
  794. starget->hostdata = vtarget;
  795. return 0;
  796. }
  797. static void
  798. mptsas_target_destroy(struct scsi_target *starget)
  799. {
  800. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  801. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  802. struct sas_rphy *rphy;
  803. struct mptsas_portinfo *p;
  804. int i;
  805. if (!starget->hostdata)
  806. return;
  807. if (starget->channel == MPTSAS_RAID_CHANNEL)
  808. goto out;
  809. rphy = dev_to_rphy(starget->dev.parent);
  810. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  811. for (i = 0; i < p->num_phys; i++) {
  812. if (p->phy_info[i].attached.sas_address !=
  813. rphy->identify.sas_address)
  814. continue;
  815. mptsas_set_starget(&p->phy_info[i], NULL);
  816. goto out;
  817. }
  818. }
  819. out:
  820. kfree(starget->hostdata);
  821. starget->hostdata = NULL;
  822. }
  823. static int
  824. mptsas_slave_alloc(struct scsi_device *sdev)
  825. {
  826. struct Scsi_Host *host = sdev->host;
  827. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  828. struct sas_rphy *rphy;
  829. struct mptsas_portinfo *p;
  830. VirtDevice *vdevice;
  831. struct scsi_target *starget;
  832. int i;
  833. vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  834. if (!vdevice) {
  835. printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
  836. hd->ioc->name, sizeof(VirtDevice));
  837. return -ENOMEM;
  838. }
  839. starget = scsi_target(sdev);
  840. vdevice->vtarget = starget->hostdata;
  841. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  842. goto out;
  843. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  844. mutex_lock(&hd->ioc->sas_topology_mutex);
  845. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  846. for (i = 0; i < p->num_phys; i++) {
  847. if (p->phy_info[i].attached.sas_address !=
  848. rphy->identify.sas_address)
  849. continue;
  850. vdevice->lun = sdev->lun;
  851. /*
  852. * Exposing hidden raid components
  853. */
  854. if (mptscsih_is_phys_disk(hd->ioc,
  855. p->phy_info[i].attached.channel,
  856. p->phy_info[i].attached.id))
  857. sdev->no_uld_attach = 1;
  858. mutex_unlock(&hd->ioc->sas_topology_mutex);
  859. goto out;
  860. }
  861. }
  862. mutex_unlock(&hd->ioc->sas_topology_mutex);
  863. kfree(vdevice);
  864. return -ENXIO;
  865. out:
  866. vdevice->vtarget->num_luns++;
  867. sdev->hostdata = vdevice;
  868. return 0;
  869. }
  870. static int
  871. mptsas_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  872. {
  873. VirtDevice *vdevice = SCpnt->device->hostdata;
  874. if (!vdevice || !vdevice->vtarget || vdevice->vtarget->deleted) {
  875. SCpnt->result = DID_NO_CONNECT << 16;
  876. done(SCpnt);
  877. return 0;
  878. }
  879. // scsi_print_command(SCpnt);
  880. return mptscsih_qcmd(SCpnt,done);
  881. }
  882. static struct scsi_host_template mptsas_driver_template = {
  883. .module = THIS_MODULE,
  884. .proc_name = "mptsas",
  885. .proc_info = mptscsih_proc_info,
  886. .name = "MPT SPI Host",
  887. .info = mptscsih_info,
  888. .queuecommand = mptsas_qcmd,
  889. .target_alloc = mptsas_target_alloc,
  890. .slave_alloc = mptsas_slave_alloc,
  891. .slave_configure = mptsas_slave_configure,
  892. .target_destroy = mptsas_target_destroy,
  893. .slave_destroy = mptscsih_slave_destroy,
  894. .change_queue_depth = mptscsih_change_queue_depth,
  895. .eh_abort_handler = mptscsih_abort,
  896. .eh_device_reset_handler = mptscsih_dev_reset,
  897. .eh_bus_reset_handler = mptscsih_bus_reset,
  898. .eh_host_reset_handler = mptscsih_host_reset,
  899. .bios_param = mptscsih_bios_param,
  900. .can_queue = MPT_FC_CAN_QUEUE,
  901. .this_id = -1,
  902. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  903. .max_sectors = 8192,
  904. .cmd_per_lun = 7,
  905. .use_clustering = ENABLE_CLUSTERING,
  906. .shost_attrs = mptscsih_host_attrs,
  907. };
  908. static int mptsas_get_linkerrors(struct sas_phy *phy)
  909. {
  910. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  911. ConfigExtendedPageHeader_t hdr;
  912. CONFIGPARMS cfg;
  913. SasPhyPage1_t *buffer;
  914. dma_addr_t dma_handle;
  915. int error;
  916. /* FIXME: only have link errors on local phys */
  917. if (!scsi_is_sas_phy_local(phy))
  918. return -EINVAL;
  919. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  920. hdr.ExtPageLength = 0;
  921. hdr.PageNumber = 1 /* page number 1*/;
  922. hdr.Reserved1 = 0;
  923. hdr.Reserved2 = 0;
  924. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  925. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  926. cfg.cfghdr.ehdr = &hdr;
  927. cfg.physAddr = -1;
  928. cfg.pageAddr = phy->identify.phy_identifier;
  929. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  930. cfg.dir = 0; /* read */
  931. cfg.timeout = 10;
  932. error = mpt_config(ioc, &cfg);
  933. if (error)
  934. return error;
  935. if (!hdr.ExtPageLength)
  936. return -ENXIO;
  937. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  938. &dma_handle);
  939. if (!buffer)
  940. return -ENOMEM;
  941. cfg.physAddr = dma_handle;
  942. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  943. error = mpt_config(ioc, &cfg);
  944. if (error)
  945. goto out_free_consistent;
  946. mptsas_print_phy_pg1(ioc, buffer);
  947. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  948. phy->running_disparity_error_count =
  949. le32_to_cpu(buffer->RunningDisparityErrorCount);
  950. phy->loss_of_dword_sync_count =
  951. le32_to_cpu(buffer->LossDwordSynchCount);
  952. phy->phy_reset_problem_count =
  953. le32_to_cpu(buffer->PhyResetProblemCount);
  954. out_free_consistent:
  955. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  956. buffer, dma_handle);
  957. return error;
  958. }
  959. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  960. MPT_FRAME_HDR *reply)
  961. {
  962. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
  963. if (reply != NULL) {
  964. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
  965. memcpy(ioc->sas_mgmt.reply, reply,
  966. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  967. }
  968. complete(&ioc->sas_mgmt.done);
  969. return 1;
  970. }
  971. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  972. {
  973. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  974. SasIoUnitControlRequest_t *req;
  975. SasIoUnitControlReply_t *reply;
  976. MPT_FRAME_HDR *mf;
  977. MPIHeader_t *hdr;
  978. unsigned long timeleft;
  979. int error = -ERESTARTSYS;
  980. /* FIXME: fusion doesn't allow non-local phy reset */
  981. if (!scsi_is_sas_phy_local(phy))
  982. return -EINVAL;
  983. /* not implemented for expanders */
  984. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  985. return -ENXIO;
  986. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  987. goto out;
  988. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  989. if (!mf) {
  990. error = -ENOMEM;
  991. goto out_unlock;
  992. }
  993. hdr = (MPIHeader_t *) mf;
  994. req = (SasIoUnitControlRequest_t *)mf;
  995. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  996. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  997. req->MsgContext = hdr->MsgContext;
  998. req->Operation = hard_reset ?
  999. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  1000. req->PhyNum = phy->identify.phy_identifier;
  1001. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  1002. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  1003. 10 * HZ);
  1004. if (!timeleft) {
  1005. /* On timeout reset the board */
  1006. mpt_free_msg_frame(ioc, mf);
  1007. mpt_HardResetHandler(ioc, CAN_SLEEP);
  1008. error = -ETIMEDOUT;
  1009. goto out_unlock;
  1010. }
  1011. /* a reply frame is expected */
  1012. if ((ioc->sas_mgmt.status &
  1013. MPT_IOCTL_STATUS_RF_VALID) == 0) {
  1014. error = -ENXIO;
  1015. goto out_unlock;
  1016. }
  1017. /* process the completed Reply Message Frame */
  1018. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  1019. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  1020. printk(MYIOC_s_INFO_FMT "%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  1021. ioc->name, __FUNCTION__, reply->IOCStatus, reply->IOCLogInfo);
  1022. error = -ENXIO;
  1023. goto out_unlock;
  1024. }
  1025. error = 0;
  1026. out_unlock:
  1027. mutex_unlock(&ioc->sas_mgmt.mutex);
  1028. out:
  1029. return error;
  1030. }
  1031. static int
  1032. mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1033. {
  1034. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1035. int i, error;
  1036. struct mptsas_portinfo *p;
  1037. struct mptsas_enclosure enclosure_info;
  1038. u64 enclosure_handle;
  1039. mutex_lock(&ioc->sas_topology_mutex);
  1040. list_for_each_entry(p, &ioc->sas_topology, list) {
  1041. for (i = 0; i < p->num_phys; i++) {
  1042. if (p->phy_info[i].attached.sas_address ==
  1043. rphy->identify.sas_address) {
  1044. enclosure_handle = p->phy_info[i].
  1045. attached.handle_enclosure;
  1046. goto found_info;
  1047. }
  1048. }
  1049. }
  1050. mutex_unlock(&ioc->sas_topology_mutex);
  1051. return -ENXIO;
  1052. found_info:
  1053. mutex_unlock(&ioc->sas_topology_mutex);
  1054. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  1055. error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  1056. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  1057. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
  1058. if (!error)
  1059. *identifier = enclosure_info.enclosure_logical_id;
  1060. return error;
  1061. }
  1062. static int
  1063. mptsas_get_bay_identifier(struct sas_rphy *rphy)
  1064. {
  1065. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1066. struct mptsas_portinfo *p;
  1067. int i, rc;
  1068. mutex_lock(&ioc->sas_topology_mutex);
  1069. list_for_each_entry(p, &ioc->sas_topology, list) {
  1070. for (i = 0; i < p->num_phys; i++) {
  1071. if (p->phy_info[i].attached.sas_address ==
  1072. rphy->identify.sas_address) {
  1073. rc = p->phy_info[i].attached.slot;
  1074. goto out;
  1075. }
  1076. }
  1077. }
  1078. rc = -ENXIO;
  1079. out:
  1080. mutex_unlock(&ioc->sas_topology_mutex);
  1081. return rc;
  1082. }
  1083. static int mptsas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1084. struct request *req)
  1085. {
  1086. MPT_ADAPTER *ioc = ((MPT_SCSI_HOST *) shost->hostdata)->ioc;
  1087. MPT_FRAME_HDR *mf;
  1088. SmpPassthroughRequest_t *smpreq;
  1089. struct request *rsp = req->next_rq;
  1090. int ret;
  1091. int flagsLength;
  1092. unsigned long timeleft;
  1093. char *psge;
  1094. dma_addr_t dma_addr_in = 0;
  1095. dma_addr_t dma_addr_out = 0;
  1096. u64 sas_address = 0;
  1097. if (!rsp) {
  1098. printk(MYIOC_s_ERR_FMT "%s: the smp response space is missing\n",
  1099. ioc->name, __FUNCTION__);
  1100. return -EINVAL;
  1101. }
  1102. /* do we need to support multiple segments? */
  1103. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1104. printk(MYIOC_s_ERR_FMT "%s: multiple segments req %u %u, rsp %u %u\n",
  1105. ioc->name, __FUNCTION__, req->bio->bi_vcnt, req->data_len,
  1106. rsp->bio->bi_vcnt, rsp->data_len);
  1107. return -EINVAL;
  1108. }
  1109. ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
  1110. if (ret)
  1111. goto out;
  1112. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1113. if (!mf) {
  1114. ret = -ENOMEM;
  1115. goto out_unlock;
  1116. }
  1117. smpreq = (SmpPassthroughRequest_t *)mf;
  1118. memset(smpreq, 0, sizeof(*smpreq));
  1119. smpreq->RequestDataLength = cpu_to_le16(req->data_len - 4);
  1120. smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
  1121. if (rphy)
  1122. sas_address = rphy->identify.sas_address;
  1123. else {
  1124. struct mptsas_portinfo *port_info;
  1125. mutex_lock(&ioc->sas_topology_mutex);
  1126. port_info = mptsas_find_portinfo_by_handle(ioc, ioc->handle);
  1127. if (port_info && port_info->phy_info)
  1128. sas_address =
  1129. port_info->phy_info[0].phy->identify.sas_address;
  1130. mutex_unlock(&ioc->sas_topology_mutex);
  1131. }
  1132. *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
  1133. psge = (char *)
  1134. (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
  1135. /* request */
  1136. flagsLength = (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  1137. MPI_SGE_FLAGS_END_OF_BUFFER |
  1138. MPI_SGE_FLAGS_DIRECTION |
  1139. mpt_addr_size()) << MPI_SGE_FLAGS_SHIFT;
  1140. flagsLength |= (req->data_len - 4);
  1141. dma_addr_out = pci_map_single(ioc->pcidev, bio_data(req->bio),
  1142. req->data_len, PCI_DMA_BIDIRECTIONAL);
  1143. if (!dma_addr_out)
  1144. goto put_mf;
  1145. mpt_add_sge(psge, flagsLength, dma_addr_out);
  1146. psge += (sizeof(u32) + sizeof(dma_addr_t));
  1147. /* response */
  1148. flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
  1149. flagsLength |= rsp->data_len + 4;
  1150. dma_addr_in = pci_map_single(ioc->pcidev, bio_data(rsp->bio),
  1151. rsp->data_len, PCI_DMA_BIDIRECTIONAL);
  1152. if (!dma_addr_in)
  1153. goto unmap;
  1154. mpt_add_sge(psge, flagsLength, dma_addr_in);
  1155. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  1156. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
  1157. if (!timeleft) {
  1158. printk(MYIOC_s_ERR_FMT "%s: smp timeout!\n", ioc->name, __FUNCTION__);
  1159. /* On timeout reset the board */
  1160. mpt_HardResetHandler(ioc, CAN_SLEEP);
  1161. ret = -ETIMEDOUT;
  1162. goto unmap;
  1163. }
  1164. mf = NULL;
  1165. if (ioc->sas_mgmt.status & MPT_IOCTL_STATUS_RF_VALID) {
  1166. SmpPassthroughReply_t *smprep;
  1167. smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
  1168. memcpy(req->sense, smprep, sizeof(*smprep));
  1169. req->sense_len = sizeof(*smprep);
  1170. } else {
  1171. printk(MYIOC_s_ERR_FMT "%s: smp passthru reply failed to be returned\n",
  1172. ioc->name, __FUNCTION__);
  1173. ret = -ENXIO;
  1174. }
  1175. unmap:
  1176. if (dma_addr_out)
  1177. pci_unmap_single(ioc->pcidev, dma_addr_out, req->data_len,
  1178. PCI_DMA_BIDIRECTIONAL);
  1179. if (dma_addr_in)
  1180. pci_unmap_single(ioc->pcidev, dma_addr_in, rsp->data_len,
  1181. PCI_DMA_BIDIRECTIONAL);
  1182. put_mf:
  1183. if (mf)
  1184. mpt_free_msg_frame(ioc, mf);
  1185. out_unlock:
  1186. mutex_unlock(&ioc->sas_mgmt.mutex);
  1187. out:
  1188. return ret;
  1189. }
  1190. static struct sas_function_template mptsas_transport_functions = {
  1191. .get_linkerrors = mptsas_get_linkerrors,
  1192. .get_enclosure_identifier = mptsas_get_enclosure_identifier,
  1193. .get_bay_identifier = mptsas_get_bay_identifier,
  1194. .phy_reset = mptsas_phy_reset,
  1195. .smp_handler = mptsas_smp_handler,
  1196. };
  1197. static struct scsi_transport_template *mptsas_transport_template;
  1198. static int
  1199. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  1200. {
  1201. ConfigExtendedPageHeader_t hdr;
  1202. CONFIGPARMS cfg;
  1203. SasIOUnitPage0_t *buffer;
  1204. dma_addr_t dma_handle;
  1205. int error, i;
  1206. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  1207. hdr.ExtPageLength = 0;
  1208. hdr.PageNumber = 0;
  1209. hdr.Reserved1 = 0;
  1210. hdr.Reserved2 = 0;
  1211. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1212. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  1213. cfg.cfghdr.ehdr = &hdr;
  1214. cfg.physAddr = -1;
  1215. cfg.pageAddr = 0;
  1216. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1217. cfg.dir = 0; /* read */
  1218. cfg.timeout = 10;
  1219. error = mpt_config(ioc, &cfg);
  1220. if (error)
  1221. goto out;
  1222. if (!hdr.ExtPageLength) {
  1223. error = -ENXIO;
  1224. goto out;
  1225. }
  1226. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1227. &dma_handle);
  1228. if (!buffer) {
  1229. error = -ENOMEM;
  1230. goto out;
  1231. }
  1232. cfg.physAddr = dma_handle;
  1233. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1234. error = mpt_config(ioc, &cfg);
  1235. if (error)
  1236. goto out_free_consistent;
  1237. port_info->num_phys = buffer->NumPhys;
  1238. port_info->phy_info = kcalloc(port_info->num_phys,
  1239. sizeof(*port_info->phy_info),GFP_KERNEL);
  1240. if (!port_info->phy_info) {
  1241. error = -ENOMEM;
  1242. goto out_free_consistent;
  1243. }
  1244. ioc->nvdata_version_persistent =
  1245. le16_to_cpu(buffer->NvdataVersionPersistent);
  1246. ioc->nvdata_version_default =
  1247. le16_to_cpu(buffer->NvdataVersionDefault);
  1248. for (i = 0; i < port_info->num_phys; i++) {
  1249. mptsas_print_phy_data(ioc, &buffer->PhyData[i]);
  1250. port_info->phy_info[i].phy_id = i;
  1251. port_info->phy_info[i].port_id =
  1252. buffer->PhyData[i].Port;
  1253. port_info->phy_info[i].negotiated_link_rate =
  1254. buffer->PhyData[i].NegotiatedLinkRate;
  1255. port_info->phy_info[i].portinfo = port_info;
  1256. port_info->phy_info[i].handle =
  1257. le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
  1258. }
  1259. out_free_consistent:
  1260. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1261. buffer, dma_handle);
  1262. out:
  1263. return error;
  1264. }
  1265. static int
  1266. mptsas_sas_io_unit_pg1(MPT_ADAPTER *ioc)
  1267. {
  1268. ConfigExtendedPageHeader_t hdr;
  1269. CONFIGPARMS cfg;
  1270. SasIOUnitPage1_t *buffer;
  1271. dma_addr_t dma_handle;
  1272. int error;
  1273. u16 device_missing_delay;
  1274. memset(&hdr, 0, sizeof(ConfigExtendedPageHeader_t));
  1275. memset(&cfg, 0, sizeof(CONFIGPARMS));
  1276. cfg.cfghdr.ehdr = &hdr;
  1277. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1278. cfg.timeout = 10;
  1279. cfg.cfghdr.ehdr->PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1280. cfg.cfghdr.ehdr->ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  1281. cfg.cfghdr.ehdr->PageVersion = MPI_SASIOUNITPAGE1_PAGEVERSION;
  1282. cfg.cfghdr.ehdr->PageNumber = 1;
  1283. error = mpt_config(ioc, &cfg);
  1284. if (error)
  1285. goto out;
  1286. if (!hdr.ExtPageLength) {
  1287. error = -ENXIO;
  1288. goto out;
  1289. }
  1290. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1291. &dma_handle);
  1292. if (!buffer) {
  1293. error = -ENOMEM;
  1294. goto out;
  1295. }
  1296. cfg.physAddr = dma_handle;
  1297. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1298. error = mpt_config(ioc, &cfg);
  1299. if (error)
  1300. goto out_free_consistent;
  1301. ioc->io_missing_delay =
  1302. le16_to_cpu(buffer->IODeviceMissingDelay);
  1303. device_missing_delay = le16_to_cpu(buffer->ReportDeviceMissingDelay);
  1304. ioc->device_missing_delay = (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_UNIT_16) ?
  1305. (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16 :
  1306. device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  1307. out_free_consistent:
  1308. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1309. buffer, dma_handle);
  1310. out:
  1311. return error;
  1312. }
  1313. static int
  1314. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  1315. u32 form, u32 form_specific)
  1316. {
  1317. ConfigExtendedPageHeader_t hdr;
  1318. CONFIGPARMS cfg;
  1319. SasPhyPage0_t *buffer;
  1320. dma_addr_t dma_handle;
  1321. int error;
  1322. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  1323. hdr.ExtPageLength = 0;
  1324. hdr.PageNumber = 0;
  1325. hdr.Reserved1 = 0;
  1326. hdr.Reserved2 = 0;
  1327. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1328. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  1329. cfg.cfghdr.ehdr = &hdr;
  1330. cfg.dir = 0; /* read */
  1331. cfg.timeout = 10;
  1332. /* Get Phy Pg 0 for each Phy. */
  1333. cfg.physAddr = -1;
  1334. cfg.pageAddr = form + form_specific;
  1335. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1336. error = mpt_config(ioc, &cfg);
  1337. if (error)
  1338. goto out;
  1339. if (!hdr.ExtPageLength) {
  1340. error = -ENXIO;
  1341. goto out;
  1342. }
  1343. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1344. &dma_handle);
  1345. if (!buffer) {
  1346. error = -ENOMEM;
  1347. goto out;
  1348. }
  1349. cfg.physAddr = dma_handle;
  1350. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1351. error = mpt_config(ioc, &cfg);
  1352. if (error)
  1353. goto out_free_consistent;
  1354. mptsas_print_phy_pg0(ioc, buffer);
  1355. phy_info->hw_link_rate = buffer->HwLinkRate;
  1356. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  1357. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  1358. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  1359. out_free_consistent:
  1360. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1361. buffer, dma_handle);
  1362. out:
  1363. return error;
  1364. }
  1365. static int
  1366. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  1367. u32 form, u32 form_specific)
  1368. {
  1369. ConfigExtendedPageHeader_t hdr;
  1370. CONFIGPARMS cfg;
  1371. SasDevicePage0_t *buffer;
  1372. dma_addr_t dma_handle;
  1373. __le64 sas_address;
  1374. int error=0;
  1375. if (ioc->sas_discovery_runtime &&
  1376. mptsas_is_end_device(device_info))
  1377. goto out;
  1378. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  1379. hdr.ExtPageLength = 0;
  1380. hdr.PageNumber = 0;
  1381. hdr.Reserved1 = 0;
  1382. hdr.Reserved2 = 0;
  1383. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1384. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  1385. cfg.cfghdr.ehdr = &hdr;
  1386. cfg.pageAddr = form + form_specific;
  1387. cfg.physAddr = -1;
  1388. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1389. cfg.dir = 0; /* read */
  1390. cfg.timeout = 10;
  1391. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  1392. error = mpt_config(ioc, &cfg);
  1393. if (error)
  1394. goto out;
  1395. if (!hdr.ExtPageLength) {
  1396. error = -ENXIO;
  1397. goto out;
  1398. }
  1399. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1400. &dma_handle);
  1401. if (!buffer) {
  1402. error = -ENOMEM;
  1403. goto out;
  1404. }
  1405. cfg.physAddr = dma_handle;
  1406. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1407. error = mpt_config(ioc, &cfg);
  1408. if (error)
  1409. goto out_free_consistent;
  1410. mptsas_print_device_pg0(ioc, buffer);
  1411. device_info->handle = le16_to_cpu(buffer->DevHandle);
  1412. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  1413. device_info->handle_enclosure =
  1414. le16_to_cpu(buffer->EnclosureHandle);
  1415. device_info->slot = le16_to_cpu(buffer->Slot);
  1416. device_info->phy_id = buffer->PhyNum;
  1417. device_info->port_id = buffer->PhysicalPort;
  1418. device_info->id = buffer->TargetID;
  1419. device_info->phys_disk_num = ~0;
  1420. device_info->channel = buffer->Bus;
  1421. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  1422. device_info->sas_address = le64_to_cpu(sas_address);
  1423. device_info->device_info =
  1424. le32_to_cpu(buffer->DeviceInfo);
  1425. out_free_consistent:
  1426. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1427. buffer, dma_handle);
  1428. out:
  1429. return error;
  1430. }
  1431. static int
  1432. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  1433. u32 form, u32 form_specific)
  1434. {
  1435. ConfigExtendedPageHeader_t hdr;
  1436. CONFIGPARMS cfg;
  1437. SasExpanderPage0_t *buffer;
  1438. dma_addr_t dma_handle;
  1439. int i, error;
  1440. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  1441. hdr.ExtPageLength = 0;
  1442. hdr.PageNumber = 0;
  1443. hdr.Reserved1 = 0;
  1444. hdr.Reserved2 = 0;
  1445. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1446. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  1447. cfg.cfghdr.ehdr = &hdr;
  1448. cfg.physAddr = -1;
  1449. cfg.pageAddr = form + form_specific;
  1450. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1451. cfg.dir = 0; /* read */
  1452. cfg.timeout = 10;
  1453. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  1454. error = mpt_config(ioc, &cfg);
  1455. if (error)
  1456. goto out;
  1457. if (!hdr.ExtPageLength) {
  1458. error = -ENXIO;
  1459. goto out;
  1460. }
  1461. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1462. &dma_handle);
  1463. if (!buffer) {
  1464. error = -ENOMEM;
  1465. goto out;
  1466. }
  1467. cfg.physAddr = dma_handle;
  1468. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1469. error = mpt_config(ioc, &cfg);
  1470. if (error)
  1471. goto out_free_consistent;
  1472. /* save config data */
  1473. port_info->num_phys = buffer->NumPhys;
  1474. port_info->phy_info = kcalloc(port_info->num_phys,
  1475. sizeof(*port_info->phy_info),GFP_KERNEL);
  1476. if (!port_info->phy_info) {
  1477. error = -ENOMEM;
  1478. goto out_free_consistent;
  1479. }
  1480. for (i = 0; i < port_info->num_phys; i++) {
  1481. port_info->phy_info[i].portinfo = port_info;
  1482. port_info->phy_info[i].handle =
  1483. le16_to_cpu(buffer->DevHandle);
  1484. }
  1485. out_free_consistent:
  1486. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1487. buffer, dma_handle);
  1488. out:
  1489. return error;
  1490. }
  1491. static int
  1492. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  1493. u32 form, u32 form_specific)
  1494. {
  1495. ConfigExtendedPageHeader_t hdr;
  1496. CONFIGPARMS cfg;
  1497. SasExpanderPage1_t *buffer;
  1498. dma_addr_t dma_handle;
  1499. int error=0;
  1500. if (ioc->sas_discovery_runtime &&
  1501. mptsas_is_end_device(&phy_info->attached))
  1502. goto out;
  1503. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  1504. hdr.ExtPageLength = 0;
  1505. hdr.PageNumber = 1;
  1506. hdr.Reserved1 = 0;
  1507. hdr.Reserved2 = 0;
  1508. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1509. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  1510. cfg.cfghdr.ehdr = &hdr;
  1511. cfg.physAddr = -1;
  1512. cfg.pageAddr = form + form_specific;
  1513. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1514. cfg.dir = 0; /* read */
  1515. cfg.timeout = 10;
  1516. error = mpt_config(ioc, &cfg);
  1517. if (error)
  1518. goto out;
  1519. if (!hdr.ExtPageLength) {
  1520. error = -ENXIO;
  1521. goto out;
  1522. }
  1523. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1524. &dma_handle);
  1525. if (!buffer) {
  1526. error = -ENOMEM;
  1527. goto out;
  1528. }
  1529. cfg.physAddr = dma_handle;
  1530. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1531. error = mpt_config(ioc, &cfg);
  1532. if (error)
  1533. goto out_free_consistent;
  1534. mptsas_print_expander_pg1(ioc, buffer);
  1535. /* save config data */
  1536. phy_info->phy_id = buffer->PhyIdentifier;
  1537. phy_info->port_id = buffer->PhysicalPort;
  1538. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  1539. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  1540. phy_info->hw_link_rate = buffer->HwLinkRate;
  1541. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  1542. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  1543. out_free_consistent:
  1544. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1545. buffer, dma_handle);
  1546. out:
  1547. return error;
  1548. }
  1549. static void
  1550. mptsas_parse_device_info(struct sas_identify *identify,
  1551. struct mptsas_devinfo *device_info)
  1552. {
  1553. u16 protocols;
  1554. identify->sas_address = device_info->sas_address;
  1555. identify->phy_identifier = device_info->phy_id;
  1556. /*
  1557. * Fill in Phy Initiator Port Protocol.
  1558. * Bits 6:3, more than one bit can be set, fall through cases.
  1559. */
  1560. protocols = device_info->device_info & 0x78;
  1561. identify->initiator_port_protocols = 0;
  1562. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  1563. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  1564. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  1565. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  1566. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  1567. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  1568. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  1569. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  1570. /*
  1571. * Fill in Phy Target Port Protocol.
  1572. * Bits 10:7, more than one bit can be set, fall through cases.
  1573. */
  1574. protocols = device_info->device_info & 0x780;
  1575. identify->target_port_protocols = 0;
  1576. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1577. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  1578. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1579. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  1580. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  1581. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  1582. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1583. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  1584. /*
  1585. * Fill in Attached device type.
  1586. */
  1587. switch (device_info->device_info &
  1588. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  1589. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  1590. identify->device_type = SAS_PHY_UNUSED;
  1591. break;
  1592. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  1593. identify->device_type = SAS_END_DEVICE;
  1594. break;
  1595. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  1596. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  1597. break;
  1598. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  1599. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  1600. break;
  1601. }
  1602. }
  1603. static int mptsas_probe_one_phy(struct device *dev,
  1604. struct mptsas_phyinfo *phy_info, int index, int local)
  1605. {
  1606. MPT_ADAPTER *ioc;
  1607. struct sas_phy *phy;
  1608. struct sas_port *port;
  1609. int error = 0;
  1610. if (!dev) {
  1611. error = -ENODEV;
  1612. goto out;
  1613. }
  1614. if (!phy_info->phy) {
  1615. phy = sas_phy_alloc(dev, index);
  1616. if (!phy) {
  1617. error = -ENOMEM;
  1618. goto out;
  1619. }
  1620. } else
  1621. phy = phy_info->phy;
  1622. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  1623. /*
  1624. * Set Negotiated link rate.
  1625. */
  1626. switch (phy_info->negotiated_link_rate) {
  1627. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  1628. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  1629. break;
  1630. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  1631. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  1632. break;
  1633. case MPI_SAS_IOUNIT0_RATE_1_5:
  1634. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1635. break;
  1636. case MPI_SAS_IOUNIT0_RATE_3_0:
  1637. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1638. break;
  1639. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  1640. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  1641. default:
  1642. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  1643. break;
  1644. }
  1645. /*
  1646. * Set Max hardware link rate.
  1647. */
  1648. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1649. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  1650. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1651. break;
  1652. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1653. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1654. break;
  1655. default:
  1656. break;
  1657. }
  1658. /*
  1659. * Set Max programmed link rate.
  1660. */
  1661. switch (phy_info->programmed_link_rate &
  1662. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1663. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  1664. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1665. break;
  1666. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1667. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1668. break;
  1669. default:
  1670. break;
  1671. }
  1672. /*
  1673. * Set Min hardware link rate.
  1674. */
  1675. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  1676. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  1677. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1678. break;
  1679. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1680. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1681. break;
  1682. default:
  1683. break;
  1684. }
  1685. /*
  1686. * Set Min programmed link rate.
  1687. */
  1688. switch (phy_info->programmed_link_rate &
  1689. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  1690. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  1691. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1692. break;
  1693. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1694. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1695. break;
  1696. default:
  1697. break;
  1698. }
  1699. if (!phy_info->phy) {
  1700. error = sas_phy_add(phy);
  1701. if (error) {
  1702. sas_phy_free(phy);
  1703. goto out;
  1704. }
  1705. phy_info->phy = phy;
  1706. }
  1707. if (!phy_info->attached.handle ||
  1708. !phy_info->port_details)
  1709. goto out;
  1710. port = mptsas_get_port(phy_info);
  1711. ioc = phy_to_ioc(phy_info->phy);
  1712. if (phy_info->sas_port_add_phy) {
  1713. if (!port) {
  1714. port = sas_port_alloc_num(dev);
  1715. if (!port) {
  1716. error = -ENOMEM;
  1717. goto out;
  1718. }
  1719. error = sas_port_add(port);
  1720. if (error) {
  1721. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1722. "%s: exit at line=%d\n", ioc->name,
  1723. __FUNCTION__, __LINE__));
  1724. goto out;
  1725. }
  1726. mptsas_set_port(ioc, phy_info, port);
  1727. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1728. "sas_port_alloc: port=%p dev=%p port_id=%d\n",
  1729. ioc->name, port, dev, port->port_identifier));
  1730. }
  1731. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_port_add_phy: phy_id=%d\n",
  1732. ioc->name, phy_info->phy_id));
  1733. sas_port_add_phy(port, phy_info->phy);
  1734. phy_info->sas_port_add_phy = 0;
  1735. }
  1736. if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
  1737. struct sas_rphy *rphy;
  1738. struct device *parent;
  1739. struct sas_identify identify;
  1740. parent = dev->parent->parent;
  1741. /*
  1742. * Let the hotplug_work thread handle processing
  1743. * the adding/removing of devices that occur
  1744. * after start of day.
  1745. */
  1746. if (ioc->sas_discovery_runtime &&
  1747. mptsas_is_end_device(&phy_info->attached))
  1748. goto out;
  1749. mptsas_parse_device_info(&identify, &phy_info->attached);
  1750. if (scsi_is_host_device(parent)) {
  1751. struct mptsas_portinfo *port_info;
  1752. int i;
  1753. mutex_lock(&ioc->sas_topology_mutex);
  1754. port_info = mptsas_find_portinfo_by_handle(ioc,
  1755. ioc->handle);
  1756. mutex_unlock(&ioc->sas_topology_mutex);
  1757. for (i = 0; i < port_info->num_phys; i++)
  1758. if (port_info->phy_info[i].identify.sas_address ==
  1759. identify.sas_address) {
  1760. sas_port_mark_backlink(port);
  1761. goto out;
  1762. }
  1763. } else if (scsi_is_sas_rphy(parent)) {
  1764. struct sas_rphy *parent_rphy = dev_to_rphy(parent);
  1765. if (identify.sas_address ==
  1766. parent_rphy->identify.sas_address) {
  1767. sas_port_mark_backlink(port);
  1768. goto out;
  1769. }
  1770. }
  1771. switch (identify.device_type) {
  1772. case SAS_END_DEVICE:
  1773. rphy = sas_end_device_alloc(port);
  1774. break;
  1775. case SAS_EDGE_EXPANDER_DEVICE:
  1776. case SAS_FANOUT_EXPANDER_DEVICE:
  1777. rphy = sas_expander_alloc(port, identify.device_type);
  1778. break;
  1779. default:
  1780. rphy = NULL;
  1781. break;
  1782. }
  1783. if (!rphy) {
  1784. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1785. "%s: exit at line=%d\n", ioc->name,
  1786. __FUNCTION__, __LINE__));
  1787. goto out;
  1788. }
  1789. rphy->identify = identify;
  1790. error = sas_rphy_add(rphy);
  1791. if (error) {
  1792. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1793. "%s: exit at line=%d\n", ioc->name,
  1794. __FUNCTION__, __LINE__));
  1795. sas_rphy_free(rphy);
  1796. goto out;
  1797. }
  1798. mptsas_set_rphy(ioc, phy_info, rphy);
  1799. }
  1800. out:
  1801. return error;
  1802. }
  1803. static int
  1804. mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
  1805. {
  1806. struct mptsas_portinfo *port_info, *hba;
  1807. int error = -ENOMEM, i;
  1808. hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1809. if (! hba)
  1810. goto out;
  1811. error = mptsas_sas_io_unit_pg0(ioc, hba);
  1812. if (error)
  1813. goto out_free_port_info;
  1814. mptsas_sas_io_unit_pg1(ioc);
  1815. mutex_lock(&ioc->sas_topology_mutex);
  1816. ioc->handle = hba->phy_info[0].handle;
  1817. port_info = mptsas_find_portinfo_by_handle(ioc, ioc->handle);
  1818. if (!port_info) {
  1819. port_info = hba;
  1820. list_add_tail(&port_info->list, &ioc->sas_topology);
  1821. } else {
  1822. for (i = 0; i < hba->num_phys; i++) {
  1823. port_info->phy_info[i].negotiated_link_rate =
  1824. hba->phy_info[i].negotiated_link_rate;
  1825. port_info->phy_info[i].handle =
  1826. hba->phy_info[i].handle;
  1827. port_info->phy_info[i].port_id =
  1828. hba->phy_info[i].port_id;
  1829. }
  1830. kfree(hba->phy_info);
  1831. kfree(hba);
  1832. hba = NULL;
  1833. }
  1834. mutex_unlock(&ioc->sas_topology_mutex);
  1835. for (i = 0; i < port_info->num_phys; i++) {
  1836. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  1837. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  1838. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  1839. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  1840. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1841. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1842. port_info->phy_info[i].handle);
  1843. port_info->phy_info[i].identify.phy_id =
  1844. port_info->phy_info[i].phy_id = i;
  1845. if (port_info->phy_info[i].attached.handle)
  1846. mptsas_sas_device_pg0(ioc,
  1847. &port_info->phy_info[i].attached,
  1848. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1849. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1850. port_info->phy_info[i].attached.handle);
  1851. }
  1852. mptsas_setup_wide_ports(ioc, port_info);
  1853. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  1854. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  1855. &port_info->phy_info[i], ioc->sas_index, 1);
  1856. return 0;
  1857. out_free_port_info:
  1858. kfree(hba);
  1859. out:
  1860. return error;
  1861. }
  1862. static int
  1863. mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
  1864. {
  1865. struct mptsas_portinfo *port_info, *p, *ex;
  1866. struct device *parent;
  1867. struct sas_rphy *rphy;
  1868. int error = -ENOMEM, i, j;
  1869. ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1870. if (!ex)
  1871. goto out;
  1872. error = mptsas_sas_expander_pg0(ioc, ex,
  1873. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  1874. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
  1875. if (error)
  1876. goto out_free_port_info;
  1877. *handle = ex->phy_info[0].handle;
  1878. mutex_lock(&ioc->sas_topology_mutex);
  1879. port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
  1880. if (!port_info) {
  1881. port_info = ex;
  1882. list_add_tail(&port_info->list, &ioc->sas_topology);
  1883. } else {
  1884. for (i = 0; i < ex->num_phys; i++) {
  1885. port_info->phy_info[i].handle =
  1886. ex->phy_info[i].handle;
  1887. port_info->phy_info[i].port_id =
  1888. ex->phy_info[i].port_id;
  1889. }
  1890. kfree(ex->phy_info);
  1891. kfree(ex);
  1892. ex = NULL;
  1893. }
  1894. mutex_unlock(&ioc->sas_topology_mutex);
  1895. for (i = 0; i < port_info->num_phys; i++) {
  1896. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  1897. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  1898. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
  1899. if (port_info->phy_info[i].identify.handle) {
  1900. mptsas_sas_device_pg0(ioc,
  1901. &port_info->phy_info[i].identify,
  1902. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1903. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1904. port_info->phy_info[i].identify.handle);
  1905. port_info->phy_info[i].identify.phy_id =
  1906. port_info->phy_info[i].phy_id;
  1907. }
  1908. if (port_info->phy_info[i].attached.handle) {
  1909. mptsas_sas_device_pg0(ioc,
  1910. &port_info->phy_info[i].attached,
  1911. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1912. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1913. port_info->phy_info[i].attached.handle);
  1914. port_info->phy_info[i].attached.phy_id =
  1915. port_info->phy_info[i].phy_id;
  1916. }
  1917. }
  1918. parent = &ioc->sh->shost_gendev;
  1919. for (i = 0; i < port_info->num_phys; i++) {
  1920. mutex_lock(&ioc->sas_topology_mutex);
  1921. list_for_each_entry(p, &ioc->sas_topology, list) {
  1922. for (j = 0; j < p->num_phys; j++) {
  1923. if (port_info->phy_info[i].identify.handle !=
  1924. p->phy_info[j].attached.handle)
  1925. continue;
  1926. rphy = mptsas_get_rphy(&p->phy_info[j]);
  1927. parent = &rphy->dev;
  1928. }
  1929. }
  1930. mutex_unlock(&ioc->sas_topology_mutex);
  1931. }
  1932. mptsas_setup_wide_ports(ioc, port_info);
  1933. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  1934. mptsas_probe_one_phy(parent, &port_info->phy_info[i],
  1935. ioc->sas_index, 0);
  1936. return 0;
  1937. out_free_port_info:
  1938. if (ex) {
  1939. kfree(ex->phy_info);
  1940. kfree(ex);
  1941. }
  1942. out:
  1943. return error;
  1944. }
  1945. /*
  1946. * mptsas_delete_expander_phys
  1947. *
  1948. *
  1949. * This will traverse topology, and remove expanders
  1950. * that are no longer present
  1951. */
  1952. static void
  1953. mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
  1954. {
  1955. struct mptsas_portinfo buffer;
  1956. struct mptsas_portinfo *port_info, *n, *parent;
  1957. struct mptsas_phyinfo *phy_info;
  1958. struct sas_port * port;
  1959. int i;
  1960. u64 expander_sas_address;
  1961. mutex_lock(&ioc->sas_topology_mutex);
  1962. list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
  1963. if (port_info->phy_info &&
  1964. (!(port_info->phy_info[0].identify.device_info &
  1965. MPI_SAS_DEVICE_INFO_SMP_TARGET)))
  1966. continue;
  1967. if (mptsas_sas_expander_pg0(ioc, &buffer,
  1968. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  1969. MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
  1970. port_info->phy_info[0].handle)) {
  1971. /*
  1972. * Obtain the port_info instance to the parent port
  1973. */
  1974. parent = mptsas_find_portinfo_by_handle(ioc,
  1975. port_info->phy_info[0].identify.handle_parent);
  1976. if (!parent)
  1977. goto next_port;
  1978. expander_sas_address =
  1979. port_info->phy_info[0].identify.sas_address;
  1980. /*
  1981. * Delete rphys in the parent that point
  1982. * to this expander. The transport layer will
  1983. * cleanup all the children.
  1984. */
  1985. phy_info = parent->phy_info;
  1986. for (i = 0; i < parent->num_phys; i++, phy_info++) {
  1987. port = mptsas_get_port(phy_info);
  1988. if (!port)
  1989. continue;
  1990. if (phy_info->attached.sas_address !=
  1991. expander_sas_address)
  1992. continue;
  1993. dsaswideprintk(ioc,
  1994. dev_printk(MYIOC_s_DEBUG_FMT, &port->dev,
  1995. "delete port (%d)\n", ioc->name, port->port_identifier));
  1996. sas_port_delete(port);
  1997. mptsas_port_delete(ioc, phy_info->port_details);
  1998. }
  1999. next_port:
  2000. phy_info = port_info->phy_info;
  2001. for (i = 0; i < port_info->num_phys; i++, phy_info++)
  2002. mptsas_port_delete(ioc, phy_info->port_details);
  2003. list_del(&port_info->list);
  2004. kfree(port_info->phy_info);
  2005. kfree(port_info);
  2006. }
  2007. /*
  2008. * Free this memory allocated from inside
  2009. * mptsas_sas_expander_pg0
  2010. */
  2011. kfree(buffer.phy_info);
  2012. }
  2013. mutex_unlock(&ioc->sas_topology_mutex);
  2014. }
  2015. /*
  2016. * Start of day discovery
  2017. */
  2018. static void
  2019. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  2020. {
  2021. u32 handle = 0xFFFF;
  2022. int i;
  2023. mutex_lock(&ioc->sas_discovery_mutex);
  2024. mptsas_probe_hba_phys(ioc);
  2025. while (!mptsas_probe_expander_phys(ioc, &handle))
  2026. ;
  2027. /*
  2028. Reporting RAID volumes.
  2029. */
  2030. if (!ioc->ir_firmware)
  2031. goto out;
  2032. if (!ioc->raid_data.pIocPg2)
  2033. goto out;
  2034. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  2035. goto out;
  2036. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  2037. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  2038. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  2039. }
  2040. out:
  2041. mutex_unlock(&ioc->sas_discovery_mutex);
  2042. }
  2043. /*
  2044. * Work queue thread to handle Runtime discovery
  2045. * Mere purpose is the hot add/delete of expanders
  2046. *(Mutex UNLOCKED)
  2047. */
  2048. static void
  2049. __mptsas_discovery_work(MPT_ADAPTER *ioc)
  2050. {
  2051. u32 handle = 0xFFFF;
  2052. ioc->sas_discovery_runtime=1;
  2053. mptsas_delete_expander_phys(ioc);
  2054. mptsas_probe_hba_phys(ioc);
  2055. while (!mptsas_probe_expander_phys(ioc, &handle))
  2056. ;
  2057. ioc->sas_discovery_runtime=0;
  2058. }
  2059. /*
  2060. * Work queue thread to handle Runtime discovery
  2061. * Mere purpose is the hot add/delete of expanders
  2062. *(Mutex LOCKED)
  2063. */
  2064. static void
  2065. mptsas_discovery_work(struct work_struct *work)
  2066. {
  2067. struct mptsas_discovery_event *ev =
  2068. container_of(work, struct mptsas_discovery_event, work);
  2069. MPT_ADAPTER *ioc = ev->ioc;
  2070. mutex_lock(&ioc->sas_discovery_mutex);
  2071. __mptsas_discovery_work(ioc);
  2072. mutex_unlock(&ioc->sas_discovery_mutex);
  2073. kfree(ev);
  2074. }
  2075. static struct mptsas_phyinfo *
  2076. mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
  2077. {
  2078. struct mptsas_portinfo *port_info;
  2079. struct mptsas_phyinfo *phy_info = NULL;
  2080. int i;
  2081. mutex_lock(&ioc->sas_topology_mutex);
  2082. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2083. for (i = 0; i < port_info->num_phys; i++) {
  2084. if (!mptsas_is_end_device(
  2085. &port_info->phy_info[i].attached))
  2086. continue;
  2087. if (port_info->phy_info[i].attached.sas_address
  2088. != sas_address)
  2089. continue;
  2090. phy_info = &port_info->phy_info[i];
  2091. break;
  2092. }
  2093. }
  2094. mutex_unlock(&ioc->sas_topology_mutex);
  2095. return phy_info;
  2096. }
  2097. static struct mptsas_phyinfo *
  2098. mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2099. {
  2100. struct mptsas_portinfo *port_info;
  2101. struct mptsas_phyinfo *phy_info = NULL;
  2102. int i;
  2103. mutex_lock(&ioc->sas_topology_mutex);
  2104. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2105. for (i = 0; i < port_info->num_phys; i++) {
  2106. if (!mptsas_is_end_device(
  2107. &port_info->phy_info[i].attached))
  2108. continue;
  2109. if (port_info->phy_info[i].attached.id != id)
  2110. continue;
  2111. if (port_info->phy_info[i].attached.channel != channel)
  2112. continue;
  2113. phy_info = &port_info->phy_info[i];
  2114. break;
  2115. }
  2116. }
  2117. mutex_unlock(&ioc->sas_topology_mutex);
  2118. return phy_info;
  2119. }
  2120. static struct mptsas_phyinfo *
  2121. mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2122. {
  2123. struct mptsas_portinfo *port_info;
  2124. struct mptsas_phyinfo *phy_info = NULL;
  2125. int i;
  2126. mutex_lock(&ioc->sas_topology_mutex);
  2127. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2128. for (i = 0; i < port_info->num_phys; i++) {
  2129. if (!mptsas_is_end_device(
  2130. &port_info->phy_info[i].attached))
  2131. continue;
  2132. if (port_info->phy_info[i].attached.phys_disk_num == ~0)
  2133. continue;
  2134. if (port_info->phy_info[i].attached.phys_disk_num != id)
  2135. continue;
  2136. if (port_info->phy_info[i].attached.channel != channel)
  2137. continue;
  2138. phy_info = &port_info->phy_info[i];
  2139. break;
  2140. }
  2141. }
  2142. mutex_unlock(&ioc->sas_topology_mutex);
  2143. return phy_info;
  2144. }
  2145. /*
  2146. * Work queue thread to clear the persitency table
  2147. */
  2148. static void
  2149. mptsas_persist_clear_table(struct work_struct *work)
  2150. {
  2151. MPT_ADAPTER *ioc = container_of(work, MPT_ADAPTER, sas_persist_task);
  2152. mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
  2153. }
  2154. static void
  2155. mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
  2156. {
  2157. int rc;
  2158. sdev->no_uld_attach = data ? 1 : 0;
  2159. rc = scsi_device_reprobe(sdev);
  2160. }
  2161. static void
  2162. mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
  2163. {
  2164. starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
  2165. mptsas_reprobe_lun);
  2166. }
  2167. static void
  2168. mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2169. {
  2170. CONFIGPARMS cfg;
  2171. ConfigPageHeader_t hdr;
  2172. dma_addr_t dma_handle;
  2173. pRaidVolumePage0_t buffer = NULL;
  2174. RaidPhysDiskPage0_t phys_disk;
  2175. int i;
  2176. struct mptsas_hotplug_event *ev;
  2177. memset(&cfg, 0 , sizeof(CONFIGPARMS));
  2178. memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
  2179. hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
  2180. cfg.pageAddr = (channel << 8) + id;
  2181. cfg.cfghdr.hdr = &hdr;
  2182. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2183. if (mpt_config(ioc, &cfg) != 0)
  2184. goto out;
  2185. if (!hdr.PageLength)
  2186. goto out;
  2187. buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
  2188. &dma_handle);
  2189. if (!buffer)
  2190. goto out;
  2191. cfg.physAddr = dma_handle;
  2192. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2193. if (mpt_config(ioc, &cfg) != 0)
  2194. goto out;
  2195. if (!(buffer->VolumeStatus.Flags &
  2196. MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
  2197. goto out;
  2198. if (!buffer->NumPhysDisks)
  2199. goto out;
  2200. for (i = 0; i < buffer->NumPhysDisks; i++) {
  2201. if (mpt_raid_phys_disk_pg0(ioc,
  2202. buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
  2203. continue;
  2204. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2205. if (!ev) {
  2206. printk(MYIOC_s_WARN_FMT "mptsas: lost hotplug event\n", ioc->name);
  2207. goto out;
  2208. }
  2209. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2210. ev->ioc = ioc;
  2211. ev->id = phys_disk.PhysDiskID;
  2212. ev->channel = phys_disk.PhysDiskBus;
  2213. ev->phys_disk_num_valid = 1;
  2214. ev->phys_disk_num = phys_disk.PhysDiskNum;
  2215. ev->event_type = MPTSAS_ADD_DEVICE;
  2216. schedule_work(&ev->work);
  2217. }
  2218. out:
  2219. if (buffer)
  2220. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
  2221. dma_handle);
  2222. }
  2223. /*
  2224. * Work queue thread to handle SAS hotplug events
  2225. */
  2226. static void
  2227. mptsas_hotplug_work(struct work_struct *work)
  2228. {
  2229. struct mptsas_hotplug_event *ev =
  2230. container_of(work, struct mptsas_hotplug_event, work);
  2231. MPT_ADAPTER *ioc = ev->ioc;
  2232. struct mptsas_phyinfo *phy_info;
  2233. struct sas_rphy *rphy;
  2234. struct sas_port *port;
  2235. struct scsi_device *sdev;
  2236. struct scsi_target * starget;
  2237. struct sas_identify identify;
  2238. char *ds = NULL;
  2239. struct mptsas_devinfo sas_device;
  2240. VirtTarget *vtarget;
  2241. VirtDevice *vdevice;
  2242. mutex_lock(&ioc->sas_discovery_mutex);
  2243. switch (ev->event_type) {
  2244. case MPTSAS_DEL_DEVICE:
  2245. phy_info = NULL;
  2246. if (ev->phys_disk_num_valid) {
  2247. if (ev->hidden_raid_component){
  2248. if (mptsas_sas_device_pg0(ioc, &sas_device,
  2249. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  2250. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2251. (ev->channel << 8) + ev->id)) {
  2252. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2253. "%s: exit at line=%d\n", ioc->name,
  2254. __FUNCTION__, __LINE__));
  2255. break;
  2256. }
  2257. phy_info = mptsas_find_phyinfo_by_sas_address(
  2258. ioc, sas_device.sas_address);
  2259. }else
  2260. phy_info = mptsas_find_phyinfo_by_phys_disk_num(
  2261. ioc, ev->channel, ev->phys_disk_num);
  2262. }
  2263. if (!phy_info)
  2264. phy_info = mptsas_find_phyinfo_by_target(ioc,
  2265. ev->channel, ev->id);
  2266. /*
  2267. * Sanity checks, for non-existing phys and remote rphys.
  2268. */
  2269. if (!phy_info){
  2270. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2271. "%s: exit at line=%d\n", ioc->name,
  2272. __FUNCTION__, __LINE__));
  2273. break;
  2274. }
  2275. if (!phy_info->port_details) {
  2276. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2277. "%s: exit at line=%d\n", ioc->name,
  2278. __FUNCTION__, __LINE__));
  2279. break;
  2280. }
  2281. rphy = mptsas_get_rphy(phy_info);
  2282. if (!rphy) {
  2283. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2284. "%s: exit at line=%d\n", ioc->name,
  2285. __FUNCTION__, __LINE__));
  2286. break;
  2287. }
  2288. port = mptsas_get_port(phy_info);
  2289. if (!port) {
  2290. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2291. "%s: exit at line=%d\n", ioc->name,
  2292. __FUNCTION__, __LINE__));
  2293. break;
  2294. }
  2295. starget = mptsas_get_starget(phy_info);
  2296. if (starget) {
  2297. vtarget = starget->hostdata;
  2298. if (!vtarget) {
  2299. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2300. "%s: exit at line=%d\n", ioc->name,
  2301. __FUNCTION__, __LINE__));
  2302. break;
  2303. }
  2304. /*
  2305. * Handling RAID components
  2306. */
  2307. if (ev->phys_disk_num_valid &&
  2308. ev->hidden_raid_component) {
  2309. printk(MYIOC_s_INFO_FMT
  2310. "RAID Hidding: channel=%d, id=%d, "
  2311. "physdsk %d \n", ioc->name, ev->channel,
  2312. ev->id, ev->phys_disk_num);
  2313. vtarget->id = ev->phys_disk_num;
  2314. vtarget->tflags |=
  2315. MPT_TARGET_FLAGS_RAID_COMPONENT;
  2316. mptsas_reprobe_target(starget, 1);
  2317. phy_info->attached.phys_disk_num =
  2318. ev->phys_disk_num;
  2319. break;
  2320. }
  2321. }
  2322. if (phy_info->attached.device_info &
  2323. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2324. ds = "ssp";
  2325. if (phy_info->attached.device_info &
  2326. MPI_SAS_DEVICE_INFO_STP_TARGET)
  2327. ds = "stp";
  2328. if (phy_info->attached.device_info &
  2329. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2330. ds = "sata";
  2331. printk(MYIOC_s_INFO_FMT
  2332. "removing %s device, channel %d, id %d, phy %d\n",
  2333. ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
  2334. dev_printk(MYIOC_s_DEBUG_FMT, &port->dev,
  2335. "delete port (%d)\n", ioc->name, port->port_identifier);
  2336. sas_port_delete(port);
  2337. mptsas_port_delete(ioc, phy_info->port_details);
  2338. break;
  2339. case MPTSAS_ADD_DEVICE:
  2340. if (ev->phys_disk_num_valid)
  2341. mpt_findImVolumes(ioc);
  2342. /*
  2343. * Refresh sas device pg0 data
  2344. */
  2345. if (mptsas_sas_device_pg0(ioc, &sas_device,
  2346. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  2347. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2348. (ev->channel << 8) + ev->id)) {
  2349. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2350. "%s: exit at line=%d\n", ioc->name,
  2351. __FUNCTION__, __LINE__));
  2352. break;
  2353. }
  2354. __mptsas_discovery_work(ioc);
  2355. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  2356. sas_device.sas_address);
  2357. if (!phy_info || !phy_info->port_details) {
  2358. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2359. "%s: exit at line=%d\n", ioc->name,
  2360. __FUNCTION__, __LINE__));
  2361. break;
  2362. }
  2363. starget = mptsas_get_starget(phy_info);
  2364. if (starget && (!ev->hidden_raid_component)){
  2365. vtarget = starget->hostdata;
  2366. if (!vtarget) {
  2367. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2368. "%s: exit at line=%d\n", ioc->name,
  2369. __FUNCTION__, __LINE__));
  2370. break;
  2371. }
  2372. /*
  2373. * Handling RAID components
  2374. */
  2375. if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2376. printk(MYIOC_s_INFO_FMT
  2377. "RAID Exposing: channel=%d, id=%d, "
  2378. "physdsk %d \n", ioc->name, ev->channel,
  2379. ev->id, ev->phys_disk_num);
  2380. vtarget->tflags &=
  2381. ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  2382. vtarget->id = ev->id;
  2383. mptsas_reprobe_target(starget, 0);
  2384. phy_info->attached.phys_disk_num = ~0;
  2385. }
  2386. break;
  2387. }
  2388. if (mptsas_get_rphy(phy_info)) {
  2389. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2390. "%s: exit at line=%d\n", ioc->name,
  2391. __FUNCTION__, __LINE__));
  2392. if (ev->channel) printk("%d\n", __LINE__);
  2393. break;
  2394. }
  2395. port = mptsas_get_port(phy_info);
  2396. if (!port) {
  2397. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2398. "%s: exit at line=%d\n", ioc->name,
  2399. __FUNCTION__, __LINE__));
  2400. break;
  2401. }
  2402. memcpy(&phy_info->attached, &sas_device,
  2403. sizeof(struct mptsas_devinfo));
  2404. if (phy_info->attached.device_info &
  2405. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2406. ds = "ssp";
  2407. if (phy_info->attached.device_info &
  2408. MPI_SAS_DEVICE_INFO_STP_TARGET)
  2409. ds = "stp";
  2410. if (phy_info->attached.device_info &
  2411. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2412. ds = "sata";
  2413. printk(MYIOC_s_INFO_FMT
  2414. "attaching %s device, channel %d, id %d, phy %d\n",
  2415. ioc->name, ds, ev->channel, ev->id, ev->phy_id);
  2416. mptsas_parse_device_info(&identify, &phy_info->attached);
  2417. rphy = sas_end_device_alloc(port);
  2418. if (!rphy) {
  2419. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2420. "%s: exit at line=%d\n", ioc->name,
  2421. __FUNCTION__, __LINE__));
  2422. break; /* non-fatal: an rphy can be added later */
  2423. }
  2424. rphy->identify = identify;
  2425. if (sas_rphy_add(rphy)) {
  2426. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2427. "%s: exit at line=%d\n", ioc->name,
  2428. __FUNCTION__, __LINE__));
  2429. sas_rphy_free(rphy);
  2430. break;
  2431. }
  2432. mptsas_set_rphy(ioc, phy_info, rphy);
  2433. break;
  2434. case MPTSAS_ADD_RAID:
  2435. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  2436. ev->id, 0);
  2437. if (sdev) {
  2438. scsi_device_put(sdev);
  2439. break;
  2440. }
  2441. printk(MYIOC_s_INFO_FMT
  2442. "attaching raid volume, channel %d, id %d\n",
  2443. ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
  2444. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL, ev->id, 0);
  2445. mpt_findImVolumes(ioc);
  2446. break;
  2447. case MPTSAS_DEL_RAID:
  2448. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  2449. ev->id, 0);
  2450. if (!sdev)
  2451. break;
  2452. printk(MYIOC_s_INFO_FMT
  2453. "removing raid volume, channel %d, id %d\n",
  2454. ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
  2455. vdevice = sdev->hostdata;
  2456. scsi_remove_device(sdev);
  2457. scsi_device_put(sdev);
  2458. mpt_findImVolumes(ioc);
  2459. break;
  2460. case MPTSAS_ADD_INACTIVE_VOLUME:
  2461. mptsas_adding_inactive_raid_components(ioc,
  2462. ev->channel, ev->id);
  2463. break;
  2464. case MPTSAS_IGNORE_EVENT:
  2465. default:
  2466. break;
  2467. }
  2468. mutex_unlock(&ioc->sas_discovery_mutex);
  2469. kfree(ev);
  2470. }
  2471. static void
  2472. mptsas_send_sas_event(MPT_ADAPTER *ioc,
  2473. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  2474. {
  2475. struct mptsas_hotplug_event *ev;
  2476. u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  2477. __le64 sas_address;
  2478. if ((device_info &
  2479. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  2480. MPI_SAS_DEVICE_INFO_STP_TARGET |
  2481. MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
  2482. return;
  2483. switch (sas_event_data->ReasonCode) {
  2484. case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
  2485. mptsas_target_reset_queue(ioc, sas_event_data);
  2486. break;
  2487. case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
  2488. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2489. if (!ev) {
  2490. printk(MYIOC_s_WARN_FMT "lost hotplug event\n", ioc->name);
  2491. break;
  2492. }
  2493. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2494. ev->ioc = ioc;
  2495. ev->handle = le16_to_cpu(sas_event_data->DevHandle);
  2496. ev->parent_handle =
  2497. le16_to_cpu(sas_event_data->ParentDevHandle);
  2498. ev->channel = sas_event_data->Bus;
  2499. ev->id = sas_event_data->TargetID;
  2500. ev->phy_id = sas_event_data->PhyNum;
  2501. memcpy(&sas_address, &sas_event_data->SASAddress,
  2502. sizeof(__le64));
  2503. ev->sas_address = le64_to_cpu(sas_address);
  2504. ev->device_info = device_info;
  2505. if (sas_event_data->ReasonCode &
  2506. MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  2507. ev->event_type = MPTSAS_ADD_DEVICE;
  2508. else
  2509. ev->event_type = MPTSAS_DEL_DEVICE;
  2510. schedule_work(&ev->work);
  2511. break;
  2512. case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
  2513. /*
  2514. * Persistent table is full.
  2515. */
  2516. INIT_WORK(&ioc->sas_persist_task,
  2517. mptsas_persist_clear_table);
  2518. schedule_work(&ioc->sas_persist_task);
  2519. break;
  2520. /*
  2521. * TODO, handle other events
  2522. */
  2523. case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  2524. case MPI_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  2525. case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  2526. case MPI_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  2527. case MPI_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  2528. case MPI_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  2529. case MPI_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  2530. default:
  2531. break;
  2532. }
  2533. }
  2534. static void
  2535. mptsas_send_raid_event(MPT_ADAPTER *ioc,
  2536. EVENT_DATA_RAID *raid_event_data)
  2537. {
  2538. struct mptsas_hotplug_event *ev;
  2539. int status = le32_to_cpu(raid_event_data->SettingsStatus);
  2540. int state = (status >> 8) & 0xff;
  2541. if (ioc->bus_type != SAS)
  2542. return;
  2543. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2544. if (!ev) {
  2545. printk(MYIOC_s_WARN_FMT "lost hotplug event\n", ioc->name);
  2546. return;
  2547. }
  2548. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2549. ev->ioc = ioc;
  2550. ev->id = raid_event_data->VolumeID;
  2551. ev->channel = raid_event_data->VolumeBus;
  2552. ev->event_type = MPTSAS_IGNORE_EVENT;
  2553. switch (raid_event_data->ReasonCode) {
  2554. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  2555. ev->phys_disk_num_valid = 1;
  2556. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2557. ev->event_type = MPTSAS_ADD_DEVICE;
  2558. break;
  2559. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  2560. ev->phys_disk_num_valid = 1;
  2561. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2562. ev->hidden_raid_component = 1;
  2563. ev->event_type = MPTSAS_DEL_DEVICE;
  2564. break;
  2565. case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
  2566. switch (state) {
  2567. case MPI_PD_STATE_ONLINE:
  2568. case MPI_PD_STATE_NOT_COMPATIBLE:
  2569. ev->phys_disk_num_valid = 1;
  2570. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2571. ev->hidden_raid_component = 1;
  2572. ev->event_type = MPTSAS_ADD_DEVICE;
  2573. break;
  2574. case MPI_PD_STATE_MISSING:
  2575. case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
  2576. case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
  2577. case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
  2578. ev->phys_disk_num_valid = 1;
  2579. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2580. ev->event_type = MPTSAS_DEL_DEVICE;
  2581. break;
  2582. default:
  2583. break;
  2584. }
  2585. break;
  2586. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  2587. ev->event_type = MPTSAS_DEL_RAID;
  2588. break;
  2589. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  2590. ev->event_type = MPTSAS_ADD_RAID;
  2591. break;
  2592. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  2593. switch (state) {
  2594. case MPI_RAIDVOL0_STATUS_STATE_FAILED:
  2595. case MPI_RAIDVOL0_STATUS_STATE_MISSING:
  2596. ev->event_type = MPTSAS_DEL_RAID;
  2597. break;
  2598. case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
  2599. case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
  2600. ev->event_type = MPTSAS_ADD_RAID;
  2601. break;
  2602. default:
  2603. break;
  2604. }
  2605. break;
  2606. default:
  2607. break;
  2608. }
  2609. schedule_work(&ev->work);
  2610. }
  2611. static void
  2612. mptsas_send_discovery_event(MPT_ADAPTER *ioc,
  2613. EVENT_DATA_SAS_DISCOVERY *discovery_data)
  2614. {
  2615. struct mptsas_discovery_event *ev;
  2616. /*
  2617. * DiscoveryStatus
  2618. *
  2619. * This flag will be non-zero when firmware
  2620. * kicks off discovery, and return to zero
  2621. * once its completed.
  2622. */
  2623. if (discovery_data->DiscoveryStatus)
  2624. return;
  2625. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2626. if (!ev)
  2627. return;
  2628. INIT_WORK(&ev->work, mptsas_discovery_work);
  2629. ev->ioc = ioc;
  2630. schedule_work(&ev->work);
  2631. };
  2632. /*
  2633. * mptsas_send_ir2_event - handle exposing hidden disk when
  2634. * an inactive raid volume is added
  2635. *
  2636. * @ioc: Pointer to MPT_ADAPTER structure
  2637. * @ir2_data
  2638. *
  2639. */
  2640. static void
  2641. mptsas_send_ir2_event(MPT_ADAPTER *ioc, PTR_MPI_EVENT_DATA_IR2 ir2_data)
  2642. {
  2643. struct mptsas_hotplug_event *ev;
  2644. if (ir2_data->ReasonCode !=
  2645. MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED)
  2646. return;
  2647. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2648. if (!ev)
  2649. return;
  2650. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2651. ev->ioc = ioc;
  2652. ev->id = ir2_data->TargetID;
  2653. ev->channel = ir2_data->Bus;
  2654. ev->event_type = MPTSAS_ADD_INACTIVE_VOLUME;
  2655. schedule_work(&ev->work);
  2656. };
  2657. static int
  2658. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  2659. {
  2660. int rc=1;
  2661. u8 event = le32_to_cpu(reply->Event) & 0xFF;
  2662. if (!ioc->sh)
  2663. goto out;
  2664. /*
  2665. * sas_discovery_ignore_events
  2666. *
  2667. * This flag is to prevent anymore processing of
  2668. * sas events once mptsas_remove function is called.
  2669. */
  2670. if (ioc->sas_discovery_ignore_events) {
  2671. rc = mptscsih_event_process(ioc, reply);
  2672. goto out;
  2673. }
  2674. switch (event) {
  2675. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  2676. mptsas_send_sas_event(ioc,
  2677. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
  2678. break;
  2679. case MPI_EVENT_INTEGRATED_RAID:
  2680. mptsas_send_raid_event(ioc,
  2681. (EVENT_DATA_RAID *)reply->Data);
  2682. break;
  2683. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  2684. INIT_WORK(&ioc->sas_persist_task,
  2685. mptsas_persist_clear_table);
  2686. schedule_work(&ioc->sas_persist_task);
  2687. break;
  2688. case MPI_EVENT_SAS_DISCOVERY:
  2689. mptsas_send_discovery_event(ioc,
  2690. (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
  2691. break;
  2692. case MPI_EVENT_IR2:
  2693. mptsas_send_ir2_event(ioc,
  2694. (PTR_MPI_EVENT_DATA_IR2)reply->Data);
  2695. break;
  2696. default:
  2697. rc = mptscsih_event_process(ioc, reply);
  2698. break;
  2699. }
  2700. out:
  2701. return rc;
  2702. }
  2703. static int
  2704. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2705. {
  2706. struct Scsi_Host *sh;
  2707. MPT_SCSI_HOST *hd;
  2708. MPT_ADAPTER *ioc;
  2709. unsigned long flags;
  2710. int ii;
  2711. int numSGE = 0;
  2712. int scale;
  2713. int ioc_cap;
  2714. int error=0;
  2715. int r;
  2716. r = mpt_attach(pdev,id);
  2717. if (r)
  2718. return r;
  2719. ioc = pci_get_drvdata(pdev);
  2720. ioc->DoneCtx = mptsasDoneCtx;
  2721. ioc->TaskCtx = mptsasTaskCtx;
  2722. ioc->InternalCtx = mptsasInternalCtx;
  2723. /* Added sanity check on readiness of the MPT adapter.
  2724. */
  2725. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  2726. printk(MYIOC_s_WARN_FMT
  2727. "Skipping because it's not operational!\n",
  2728. ioc->name);
  2729. error = -ENODEV;
  2730. goto out_mptsas_probe;
  2731. }
  2732. if (!ioc->active) {
  2733. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  2734. ioc->name);
  2735. error = -ENODEV;
  2736. goto out_mptsas_probe;
  2737. }
  2738. /* Sanity check - ensure at least 1 port is INITIATOR capable
  2739. */
  2740. ioc_cap = 0;
  2741. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  2742. if (ioc->pfacts[ii].ProtocolFlags &
  2743. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  2744. ioc_cap++;
  2745. }
  2746. if (!ioc_cap) {
  2747. printk(MYIOC_s_WARN_FMT
  2748. "Skipping ioc=%p because SCSI Initiator mode "
  2749. "is NOT enabled!\n", ioc->name, ioc);
  2750. return 0;
  2751. }
  2752. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  2753. if (!sh) {
  2754. printk(MYIOC_s_WARN_FMT
  2755. "Unable to register controller with SCSI subsystem\n",
  2756. ioc->name);
  2757. error = -1;
  2758. goto out_mptsas_probe;
  2759. }
  2760. spin_lock_irqsave(&ioc->FreeQlock, flags);
  2761. /* Attach the SCSI Host to the IOC structure
  2762. */
  2763. ioc->sh = sh;
  2764. sh->io_port = 0;
  2765. sh->n_io_port = 0;
  2766. sh->irq = 0;
  2767. /* set 16 byte cdb's */
  2768. sh->max_cmd_len = 16;
  2769. sh->max_id = ioc->pfacts[0].PortSCSIID;
  2770. sh->max_lun = max_lun;
  2771. sh->transportt = mptsas_transport_template;
  2772. sh->this_id = ioc->pfacts[0].PortSCSIID;
  2773. /* Required entry.
  2774. */
  2775. sh->unique_id = ioc->id;
  2776. INIT_LIST_HEAD(&ioc->sas_topology);
  2777. mutex_init(&ioc->sas_topology_mutex);
  2778. mutex_init(&ioc->sas_discovery_mutex);
  2779. mutex_init(&ioc->sas_mgmt.mutex);
  2780. init_completion(&ioc->sas_mgmt.done);
  2781. /* Verify that we won't exceed the maximum
  2782. * number of chain buffers
  2783. * We can optimize: ZZ = req_sz/sizeof(SGE)
  2784. * For 32bit SGE's:
  2785. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  2786. * + (req_sz - 64)/sizeof(SGE)
  2787. * A slightly different algorithm is required for
  2788. * 64bit SGEs.
  2789. */
  2790. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  2791. if (sizeof(dma_addr_t) == sizeof(u64)) {
  2792. numSGE = (scale - 1) *
  2793. (ioc->facts.MaxChainDepth-1) + scale +
  2794. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  2795. sizeof(u32));
  2796. } else {
  2797. numSGE = 1 + (scale - 1) *
  2798. (ioc->facts.MaxChainDepth-1) + scale +
  2799. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  2800. sizeof(u32));
  2801. }
  2802. if (numSGE < sh->sg_tablesize) {
  2803. /* Reset this value */
  2804. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2805. "Resetting sg_tablesize to %d from %d\n",
  2806. ioc->name, numSGE, sh->sg_tablesize));
  2807. sh->sg_tablesize = numSGE;
  2808. }
  2809. hd = (MPT_SCSI_HOST *) sh->hostdata;
  2810. hd->ioc = ioc;
  2811. /* SCSI needs scsi_cmnd lookup table!
  2812. * (with size equal to req_depth*PtrSz!)
  2813. */
  2814. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  2815. if (!hd->ScsiLookup) {
  2816. error = -ENOMEM;
  2817. goto out_mptsas_probe;
  2818. }
  2819. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
  2820. ioc->name, hd->ScsiLookup));
  2821. /* Clear the TM flags
  2822. */
  2823. hd->tmPending = 0;
  2824. hd->tmState = TM_STATE_NONE;
  2825. hd->resetPending = 0;
  2826. hd->abortSCpnt = NULL;
  2827. /* Clear the pointer used to store
  2828. * single-threaded commands, i.e., those
  2829. * issued during a bus scan, dv and
  2830. * configuration pages.
  2831. */
  2832. hd->cmdPtr = NULL;
  2833. /* Initialize this SCSI Hosts' timers
  2834. * To use, set the timer expires field
  2835. * and add_timer
  2836. */
  2837. init_timer(&hd->timer);
  2838. hd->timer.data = (unsigned long) hd;
  2839. hd->timer.function = mptscsih_timer_expired;
  2840. ioc->sas_data.ptClear = mpt_pt_clear;
  2841. init_waitqueue_head(&hd->scandv_waitq);
  2842. hd->scandv_wait_done = 0;
  2843. hd->last_queue_full = 0;
  2844. INIT_LIST_HEAD(&hd->target_reset_list);
  2845. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  2846. if (ioc->sas_data.ptClear==1) {
  2847. mptbase_sas_persist_operation(
  2848. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  2849. }
  2850. error = scsi_add_host(sh, &ioc->pcidev->dev);
  2851. if (error) {
  2852. dprintk(ioc, printk(MYIOC_s_ERR_FMT
  2853. "scsi_add_host failed\n", ioc->name));
  2854. goto out_mptsas_probe;
  2855. }
  2856. mptsas_scan_sas_topology(ioc);
  2857. return 0;
  2858. out_mptsas_probe:
  2859. mptscsih_remove(pdev);
  2860. return error;
  2861. }
  2862. static void __devexit mptsas_remove(struct pci_dev *pdev)
  2863. {
  2864. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  2865. struct mptsas_portinfo *p, *n;
  2866. int i;
  2867. ioc->sas_discovery_ignore_events = 1;
  2868. sas_remove_host(ioc->sh);
  2869. mutex_lock(&ioc->sas_topology_mutex);
  2870. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  2871. list_del(&p->list);
  2872. for (i = 0 ; i < p->num_phys ; i++)
  2873. mptsas_port_delete(ioc, p->phy_info[i].port_details);
  2874. kfree(p->phy_info);
  2875. kfree(p);
  2876. }
  2877. mutex_unlock(&ioc->sas_topology_mutex);
  2878. mptscsih_remove(pdev);
  2879. }
  2880. static struct pci_device_id mptsas_pci_table[] = {
  2881. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
  2882. PCI_ANY_ID, PCI_ANY_ID },
  2883. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
  2884. PCI_ANY_ID, PCI_ANY_ID },
  2885. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
  2886. PCI_ANY_ID, PCI_ANY_ID },
  2887. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
  2888. PCI_ANY_ID, PCI_ANY_ID },
  2889. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
  2890. PCI_ANY_ID, PCI_ANY_ID },
  2891. {0} /* Terminating entry */
  2892. };
  2893. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  2894. static struct pci_driver mptsas_driver = {
  2895. .name = "mptsas",
  2896. .id_table = mptsas_pci_table,
  2897. .probe = mptsas_probe,
  2898. .remove = __devexit_p(mptsas_remove),
  2899. .shutdown = mptscsih_shutdown,
  2900. #ifdef CONFIG_PM
  2901. .suspend = mptscsih_suspend,
  2902. .resume = mptscsih_resume,
  2903. #endif
  2904. };
  2905. static int __init
  2906. mptsas_init(void)
  2907. {
  2908. int error;
  2909. show_mptmod_ver(my_NAME, my_VERSION);
  2910. mptsas_transport_template =
  2911. sas_attach_transport(&mptsas_transport_functions);
  2912. if (!mptsas_transport_template)
  2913. return -ENODEV;
  2914. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
  2915. mptsasTaskCtx = mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER);
  2916. mptsasInternalCtx =
  2917. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
  2918. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
  2919. mpt_event_register(mptsasDoneCtx, mptsas_event_process);
  2920. mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset);
  2921. error = pci_register_driver(&mptsas_driver);
  2922. if (error)
  2923. sas_release_transport(mptsas_transport_template);
  2924. return error;
  2925. }
  2926. static void __exit
  2927. mptsas_exit(void)
  2928. {
  2929. pci_unregister_driver(&mptsas_driver);
  2930. sas_release_transport(mptsas_transport_template);
  2931. mpt_reset_deregister(mptsasDoneCtx);
  2932. mpt_event_deregister(mptsasDoneCtx);
  2933. mpt_deregister(mptsasMgmtCtx);
  2934. mpt_deregister(mptsasInternalCtx);
  2935. mpt_deregister(mptsasTaskCtx);
  2936. mpt_deregister(mptsasDoneCtx);
  2937. }
  2938. module_init(mptsas_init);
  2939. module_exit(mptsas_exit);