mpt3sas_ctl.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674
  1. /*
  2. * Management Module Support for MPT (Message Passing Technology) based
  3. * controllers
  4. *
  5. * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
  6. * Copyright (C) 2012-2014 LSI Corporation
  7. * Copyright (C) 2013-2014 Avago Technologies
  8. * (mailto: MPT-FusionLinux.pdl@avagotech.com)
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  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. *
  20. * NO WARRANTY
  21. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  22. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  23. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  24. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  25. * solely responsible for determining the appropriateness of using and
  26. * distributing the Program and assumes all risks associated with its
  27. * exercise of rights under this Agreement, including but not limited to
  28. * the risks and costs of program errors, damage to or loss of data,
  29. * programs or equipment, and unavailability or interruption of operations.
  30. * DISCLAIMER OF LIABILITY
  31. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/slab.h>
  48. #include <linux/types.h>
  49. #include <linux/pci.h>
  50. #include <linux/delay.h>
  51. #include <linux/compat.h>
  52. #include <linux/poll.h>
  53. #include <linux/io.h>
  54. #include <linux/uaccess.h>
  55. #include "mpt3sas_base.h"
  56. #include "mpt3sas_ctl.h"
  57. static struct fasync_struct *async_queue;
  58. static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
  59. /**
  60. * enum block_state - blocking state
  61. * @NON_BLOCKING: non blocking
  62. * @BLOCKING: blocking
  63. *
  64. * These states are for ioctls that need to wait for a response
  65. * from firmware, so they probably require sleep.
  66. */
  67. enum block_state {
  68. NON_BLOCKING,
  69. BLOCKING,
  70. };
  71. /**
  72. * _ctl_display_some_debug - debug routine
  73. * @ioc: per adapter object
  74. * @smid: system request message index
  75. * @calling_function_name: string pass from calling function
  76. * @mpi_reply: reply message frame
  77. * Context: none.
  78. *
  79. * Function for displaying debug info helpful when debugging issues
  80. * in this module.
  81. */
  82. static void
  83. _ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  84. char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
  85. {
  86. Mpi2ConfigRequest_t *mpi_request;
  87. char *desc = NULL;
  88. if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
  89. return;
  90. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  91. switch (mpi_request->Function) {
  92. case MPI2_FUNCTION_SCSI_IO_REQUEST:
  93. {
  94. Mpi2SCSIIORequest_t *scsi_request =
  95. (Mpi2SCSIIORequest_t *)mpi_request;
  96. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  97. "scsi_io, cmd(0x%02x), cdb_len(%d)",
  98. scsi_request->CDB.CDB32[0],
  99. le16_to_cpu(scsi_request->IoFlags) & 0xF);
  100. desc = ioc->tmp_string;
  101. break;
  102. }
  103. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  104. desc = "task_mgmt";
  105. break;
  106. case MPI2_FUNCTION_IOC_INIT:
  107. desc = "ioc_init";
  108. break;
  109. case MPI2_FUNCTION_IOC_FACTS:
  110. desc = "ioc_facts";
  111. break;
  112. case MPI2_FUNCTION_CONFIG:
  113. {
  114. Mpi2ConfigRequest_t *config_request =
  115. (Mpi2ConfigRequest_t *)mpi_request;
  116. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  117. "config, type(0x%02x), ext_type(0x%02x), number(%d)",
  118. (config_request->Header.PageType &
  119. MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
  120. config_request->Header.PageNumber);
  121. desc = ioc->tmp_string;
  122. break;
  123. }
  124. case MPI2_FUNCTION_PORT_FACTS:
  125. desc = "port_facts";
  126. break;
  127. case MPI2_FUNCTION_PORT_ENABLE:
  128. desc = "port_enable";
  129. break;
  130. case MPI2_FUNCTION_EVENT_NOTIFICATION:
  131. desc = "event_notification";
  132. break;
  133. case MPI2_FUNCTION_FW_DOWNLOAD:
  134. desc = "fw_download";
  135. break;
  136. case MPI2_FUNCTION_FW_UPLOAD:
  137. desc = "fw_upload";
  138. break;
  139. case MPI2_FUNCTION_RAID_ACTION:
  140. desc = "raid_action";
  141. break;
  142. case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  143. {
  144. Mpi2SCSIIORequest_t *scsi_request =
  145. (Mpi2SCSIIORequest_t *)mpi_request;
  146. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  147. "raid_pass, cmd(0x%02x), cdb_len(%d)",
  148. scsi_request->CDB.CDB32[0],
  149. le16_to_cpu(scsi_request->IoFlags) & 0xF);
  150. desc = ioc->tmp_string;
  151. break;
  152. }
  153. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  154. desc = "sas_iounit_cntl";
  155. break;
  156. case MPI2_FUNCTION_SATA_PASSTHROUGH:
  157. desc = "sata_pass";
  158. break;
  159. case MPI2_FUNCTION_DIAG_BUFFER_POST:
  160. desc = "diag_buffer_post";
  161. break;
  162. case MPI2_FUNCTION_DIAG_RELEASE:
  163. desc = "diag_release";
  164. break;
  165. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  166. desc = "smp_passthrough";
  167. break;
  168. }
  169. if (!desc)
  170. return;
  171. pr_info(MPT3SAS_FMT "%s: %s, smid(%d)\n",
  172. ioc->name, calling_function_name, desc, smid);
  173. if (!mpi_reply)
  174. return;
  175. if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
  176. pr_info(MPT3SAS_FMT
  177. "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
  178. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  179. le32_to_cpu(mpi_reply->IOCLogInfo));
  180. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  181. mpi_request->Function ==
  182. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  183. Mpi2SCSIIOReply_t *scsi_reply =
  184. (Mpi2SCSIIOReply_t *)mpi_reply;
  185. struct _sas_device *sas_device = NULL;
  186. struct _pcie_device *pcie_device = NULL;
  187. sas_device = mpt3sas_get_sdev_by_handle(ioc,
  188. le16_to_cpu(scsi_reply->DevHandle));
  189. if (sas_device) {
  190. pr_warn(MPT3SAS_FMT "\tsas_address(0x%016llx), phy(%d)\n",
  191. ioc->name, (unsigned long long)
  192. sas_device->sas_address, sas_device->phy);
  193. pr_warn(MPT3SAS_FMT
  194. "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
  195. ioc->name, (unsigned long long)
  196. sas_device->enclosure_logical_id, sas_device->slot);
  197. sas_device_put(sas_device);
  198. }
  199. if (!sas_device) {
  200. pcie_device = mpt3sas_get_pdev_by_handle(ioc,
  201. le16_to_cpu(scsi_reply->DevHandle));
  202. if (pcie_device) {
  203. pr_warn(MPT3SAS_FMT
  204. "\tWWID(0x%016llx), port(%d)\n", ioc->name,
  205. (unsigned long long)pcie_device->wwid,
  206. pcie_device->port_num);
  207. if (pcie_device->enclosure_handle != 0)
  208. pr_warn(MPT3SAS_FMT
  209. "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
  210. ioc->name, (unsigned long long)
  211. pcie_device->enclosure_logical_id,
  212. pcie_device->slot);
  213. pcie_device_put(pcie_device);
  214. }
  215. }
  216. if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
  217. pr_info(MPT3SAS_FMT
  218. "\tscsi_state(0x%02x), scsi_status"
  219. "(0x%02x)\n", ioc->name,
  220. scsi_reply->SCSIState,
  221. scsi_reply->SCSIStatus);
  222. }
  223. }
  224. /**
  225. * mpt3sas_ctl_done - ctl module completion routine
  226. * @ioc: per adapter object
  227. * @smid: system request message index
  228. * @msix_index: MSIX table index supplied by the OS
  229. * @reply: reply message frame(lower 32bit addr)
  230. * Context: none.
  231. *
  232. * The callback handler when using ioc->ctl_cb_idx.
  233. *
  234. * Return: 1 meaning mf should be freed from _base_interrupt
  235. * 0 means the mf is freed from this function.
  236. */
  237. u8
  238. mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  239. u32 reply)
  240. {
  241. MPI2DefaultReply_t *mpi_reply;
  242. Mpi2SCSIIOReply_t *scsiio_reply;
  243. Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
  244. const void *sense_data;
  245. u32 sz;
  246. if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
  247. return 1;
  248. if (ioc->ctl_cmds.smid != smid)
  249. return 1;
  250. ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
  251. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  252. if (mpi_reply) {
  253. memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  254. ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
  255. /* get sense data */
  256. if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  257. mpi_reply->Function ==
  258. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  259. scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
  260. if (scsiio_reply->SCSIState &
  261. MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  262. sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  263. le32_to_cpu(scsiio_reply->SenseCount));
  264. sense_data = mpt3sas_base_get_sense_buffer(ioc,
  265. smid);
  266. memcpy(ioc->ctl_cmds.sense, sense_data, sz);
  267. }
  268. }
  269. /*
  270. * Get Error Response data for NVMe device. The ctl_cmds.sense
  271. * buffer is used to store the Error Response data.
  272. */
  273. if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
  274. nvme_error_reply =
  275. (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
  276. sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
  277. le16_to_cpu(nvme_error_reply->ErrorResponseCount));
  278. sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
  279. memcpy(ioc->ctl_cmds.sense, sense_data, sz);
  280. }
  281. }
  282. _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
  283. ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
  284. complete(&ioc->ctl_cmds.done);
  285. return 1;
  286. }
  287. /**
  288. * _ctl_check_event_type - determines when an event needs logging
  289. * @ioc: per adapter object
  290. * @event: firmware event
  291. *
  292. * The bitmask in ioc->event_type[] indicates which events should be
  293. * be saved in the driver event_log. This bitmask is set by application.
  294. *
  295. * Return: 1 when event should be captured, or zero means no match.
  296. */
  297. static int
  298. _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
  299. {
  300. u16 i;
  301. u32 desired_event;
  302. if (event >= 128 || !event || !ioc->event_log)
  303. return 0;
  304. desired_event = (1 << (event % 32));
  305. if (!desired_event)
  306. desired_event = 1;
  307. i = event / 32;
  308. return desired_event & ioc->event_type[i];
  309. }
  310. /**
  311. * mpt3sas_ctl_add_to_event_log - add event
  312. * @ioc: per adapter object
  313. * @mpi_reply: reply message frame
  314. */
  315. void
  316. mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
  317. Mpi2EventNotificationReply_t *mpi_reply)
  318. {
  319. struct MPT3_IOCTL_EVENTS *event_log;
  320. u16 event;
  321. int i;
  322. u32 sz, event_data_sz;
  323. u8 send_aen = 0;
  324. if (!ioc->event_log)
  325. return;
  326. event = le16_to_cpu(mpi_reply->Event);
  327. if (_ctl_check_event_type(ioc, event)) {
  328. /* insert entry into circular event_log */
  329. i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
  330. event_log = ioc->event_log;
  331. event_log[i].event = event;
  332. event_log[i].context = ioc->event_context++;
  333. event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
  334. sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
  335. memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
  336. memcpy(event_log[i].data, mpi_reply->EventData, sz);
  337. send_aen = 1;
  338. }
  339. /* This aen_event_read_flag flag is set until the
  340. * application has read the event log.
  341. * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
  342. */
  343. if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
  344. (send_aen && !ioc->aen_event_read_flag)) {
  345. ioc->aen_event_read_flag = 1;
  346. wake_up_interruptible(&ctl_poll_wait);
  347. if (async_queue)
  348. kill_fasync(&async_queue, SIGIO, POLL_IN);
  349. }
  350. }
  351. /**
  352. * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
  353. * @ioc: per adapter object
  354. * @msix_index: MSIX table index supplied by the OS
  355. * @reply: reply message frame(lower 32bit addr)
  356. * Context: interrupt.
  357. *
  358. * This function merely adds a new work task into ioc->firmware_event_thread.
  359. * The tasks are worked from _firmware_event_work in user context.
  360. *
  361. * Return: 1 meaning mf should be freed from _base_interrupt
  362. * 0 means the mf is freed from this function.
  363. */
  364. u8
  365. mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
  366. u32 reply)
  367. {
  368. Mpi2EventNotificationReply_t *mpi_reply;
  369. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  370. if (mpi_reply)
  371. mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
  372. return 1;
  373. }
  374. /**
  375. * _ctl_verify_adapter - validates ioc_number passed from application
  376. * @ioc_number: ?
  377. * @iocpp: The ioc pointer is returned in this.
  378. * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
  379. * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
  380. *
  381. * Return: (-1) means error, else ioc_number.
  382. */
  383. static int
  384. _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
  385. int mpi_version)
  386. {
  387. struct MPT3SAS_ADAPTER *ioc;
  388. int version = 0;
  389. /* global ioc lock to protect controller on list operations */
  390. spin_lock(&gioc_lock);
  391. list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
  392. if (ioc->id != ioc_number)
  393. continue;
  394. /* Check whether this ioctl command is from right
  395. * ioctl device or not, if not continue the search.
  396. */
  397. version = ioc->hba_mpi_version_belonged;
  398. /* MPI25_VERSION and MPI26_VERSION uses same ioctl
  399. * device.
  400. */
  401. if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
  402. if ((version == MPI25_VERSION) ||
  403. (version == MPI26_VERSION))
  404. goto out;
  405. else
  406. continue;
  407. } else {
  408. if (version != mpi_version)
  409. continue;
  410. }
  411. out:
  412. spin_unlock(&gioc_lock);
  413. *iocpp = ioc;
  414. return ioc_number;
  415. }
  416. spin_unlock(&gioc_lock);
  417. *iocpp = NULL;
  418. return -1;
  419. }
  420. /**
  421. * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
  422. * @ioc: per adapter object
  423. *
  424. * The handler for doing any required cleanup or initialization.
  425. */
  426. void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
  427. {
  428. int i;
  429. u8 issue_reset;
  430. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  431. "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
  432. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  433. if (!(ioc->diag_buffer_status[i] &
  434. MPT3_DIAG_BUFFER_IS_REGISTERED))
  435. continue;
  436. if ((ioc->diag_buffer_status[i] &
  437. MPT3_DIAG_BUFFER_IS_RELEASED))
  438. continue;
  439. mpt3sas_send_diag_release(ioc, i, &issue_reset);
  440. }
  441. }
  442. /**
  443. * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
  444. * @ioc: per adapter object
  445. *
  446. * The handler for doing any required cleanup or initialization.
  447. */
  448. void mpt3sas_ctl_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
  449. {
  450. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  451. "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
  452. if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
  453. ioc->ctl_cmds.status |= MPT3_CMD_RESET;
  454. mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
  455. complete(&ioc->ctl_cmds.done);
  456. }
  457. }
  458. /**
  459. * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
  460. * @ioc: per adapter object
  461. *
  462. * The handler for doing any required cleanup or initialization.
  463. */
  464. void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
  465. {
  466. int i;
  467. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  468. "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
  469. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  470. if (!(ioc->diag_buffer_status[i] &
  471. MPT3_DIAG_BUFFER_IS_REGISTERED))
  472. continue;
  473. if ((ioc->diag_buffer_status[i] &
  474. MPT3_DIAG_BUFFER_IS_RELEASED))
  475. continue;
  476. ioc->diag_buffer_status[i] |=
  477. MPT3_DIAG_BUFFER_IS_DIAG_RESET;
  478. }
  479. }
  480. /**
  481. * _ctl_fasync -
  482. * @fd: ?
  483. * @filep: ?
  484. * @mode: ?
  485. *
  486. * Called when application request fasyn callback handler.
  487. */
  488. static int
  489. _ctl_fasync(int fd, struct file *filep, int mode)
  490. {
  491. return fasync_helper(fd, filep, mode, &async_queue);
  492. }
  493. /**
  494. * _ctl_poll -
  495. * @filep: ?
  496. * @wait: ?
  497. *
  498. */
  499. static __poll_t
  500. _ctl_poll(struct file *filep, poll_table *wait)
  501. {
  502. struct MPT3SAS_ADAPTER *ioc;
  503. poll_wait(filep, &ctl_poll_wait, wait);
  504. /* global ioc lock to protect controller on list operations */
  505. spin_lock(&gioc_lock);
  506. list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
  507. if (ioc->aen_event_read_flag) {
  508. spin_unlock(&gioc_lock);
  509. return EPOLLIN | EPOLLRDNORM;
  510. }
  511. }
  512. spin_unlock(&gioc_lock);
  513. return 0;
  514. }
  515. /**
  516. * _ctl_set_task_mid - assign an active smid to tm request
  517. * @ioc: per adapter object
  518. * @karg: (struct mpt3_ioctl_command)
  519. * @tm_request: pointer to mf from user space
  520. *
  521. * Return: 0 when an smid if found, else fail.
  522. * during failure, the reply frame is filled.
  523. */
  524. static int
  525. _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
  526. Mpi2SCSITaskManagementRequest_t *tm_request)
  527. {
  528. u8 found = 0;
  529. u16 smid;
  530. u16 handle;
  531. struct scsi_cmnd *scmd;
  532. struct MPT3SAS_DEVICE *priv_data;
  533. Mpi2SCSITaskManagementReply_t *tm_reply;
  534. u32 sz;
  535. u32 lun;
  536. char *desc = NULL;
  537. if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
  538. desc = "abort_task";
  539. else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
  540. desc = "query_task";
  541. else
  542. return 0;
  543. lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
  544. handle = le16_to_cpu(tm_request->DevHandle);
  545. for (smid = ioc->scsiio_depth; smid && !found; smid--) {
  546. struct scsiio_tracker *st;
  547. scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
  548. if (!scmd)
  549. continue;
  550. if (lun != scmd->device->lun)
  551. continue;
  552. priv_data = scmd->device->hostdata;
  553. if (priv_data->sas_target == NULL)
  554. continue;
  555. if (priv_data->sas_target->handle != handle)
  556. continue;
  557. st = scsi_cmd_priv(scmd);
  558. tm_request->TaskMID = cpu_to_le16(st->smid);
  559. found = 1;
  560. }
  561. if (!found) {
  562. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  563. "%s: handle(0x%04x), lun(%d), no active mid!!\n",
  564. ioc->name,
  565. desc, le16_to_cpu(tm_request->DevHandle), lun));
  566. tm_reply = ioc->ctl_cmds.reply;
  567. tm_reply->DevHandle = tm_request->DevHandle;
  568. tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  569. tm_reply->TaskType = tm_request->TaskType;
  570. tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
  571. tm_reply->VP_ID = tm_request->VP_ID;
  572. tm_reply->VF_ID = tm_request->VF_ID;
  573. sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
  574. if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
  575. sz))
  576. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  577. __LINE__, __func__);
  578. return 1;
  579. }
  580. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  581. "%s: handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
  582. desc, le16_to_cpu(tm_request->DevHandle), lun,
  583. le16_to_cpu(tm_request->TaskMID)));
  584. return 0;
  585. }
  586. /**
  587. * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
  588. * @ioc: per adapter object
  589. * @karg: (struct mpt3_ioctl_command)
  590. * @mf: pointer to mf in user space
  591. */
  592. static long
  593. _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
  594. void __user *mf)
  595. {
  596. MPI2RequestHeader_t *mpi_request = NULL, *request;
  597. MPI2DefaultReply_t *mpi_reply;
  598. Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
  599. struct _pcie_device *pcie_device = NULL;
  600. u32 ioc_state;
  601. u16 smid;
  602. unsigned long timeout;
  603. u8 issue_reset;
  604. u32 sz, sz_arg;
  605. void *psge;
  606. void *data_out = NULL;
  607. dma_addr_t data_out_dma = 0;
  608. size_t data_out_sz = 0;
  609. void *data_in = NULL;
  610. dma_addr_t data_in_dma = 0;
  611. size_t data_in_sz = 0;
  612. long ret;
  613. u16 wait_state_count;
  614. u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
  615. u8 tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
  616. issue_reset = 0;
  617. if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
  618. pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
  619. ioc->name, __func__);
  620. ret = -EAGAIN;
  621. goto out;
  622. }
  623. wait_state_count = 0;
  624. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  625. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  626. if (wait_state_count++ == 10) {
  627. pr_err(MPT3SAS_FMT
  628. "%s: failed due to ioc not operational\n",
  629. ioc->name, __func__);
  630. ret = -EFAULT;
  631. goto out;
  632. }
  633. ssleep(1);
  634. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  635. pr_info(MPT3SAS_FMT
  636. "%s: waiting for operational state(count=%d)\n",
  637. ioc->name,
  638. __func__, wait_state_count);
  639. }
  640. if (wait_state_count)
  641. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  642. ioc->name, __func__);
  643. mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
  644. if (!mpi_request) {
  645. pr_err(MPT3SAS_FMT
  646. "%s: failed obtaining a memory for mpi_request\n",
  647. ioc->name, __func__);
  648. ret = -ENOMEM;
  649. goto out;
  650. }
  651. /* Check for overflow and wraparound */
  652. if (karg.data_sge_offset * 4 > ioc->request_sz ||
  653. karg.data_sge_offset > (UINT_MAX / 4)) {
  654. ret = -EINVAL;
  655. goto out;
  656. }
  657. /* copy in request message frame from user */
  658. if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
  659. pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
  660. __func__);
  661. ret = -EFAULT;
  662. goto out;
  663. }
  664. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  665. smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
  666. if (!smid) {
  667. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  668. ioc->name, __func__);
  669. ret = -EAGAIN;
  670. goto out;
  671. }
  672. } else {
  673. /* Use first reserved smid for passthrough ioctls */
  674. smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
  675. }
  676. ret = 0;
  677. ioc->ctl_cmds.status = MPT3_CMD_PENDING;
  678. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  679. request = mpt3sas_base_get_msg_frame(ioc, smid);
  680. memcpy(request, mpi_request, karg.data_sge_offset*4);
  681. ioc->ctl_cmds.smid = smid;
  682. data_out_sz = karg.data_out_size;
  683. data_in_sz = karg.data_in_size;
  684. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  685. mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  686. mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
  687. mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
  688. mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
  689. device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
  690. if (!device_handle || (device_handle >
  691. ioc->facts.MaxDevHandle)) {
  692. ret = -EINVAL;
  693. mpt3sas_base_free_smid(ioc, smid);
  694. goto out;
  695. }
  696. }
  697. /* obtain dma-able memory for data transfer */
  698. if (data_out_sz) /* WRITE */ {
  699. data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
  700. &data_out_dma);
  701. if (!data_out) {
  702. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  703. __LINE__, __func__);
  704. ret = -ENOMEM;
  705. mpt3sas_base_free_smid(ioc, smid);
  706. goto out;
  707. }
  708. if (copy_from_user(data_out, karg.data_out_buf_ptr,
  709. data_out_sz)) {
  710. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  711. __LINE__, __func__);
  712. ret = -EFAULT;
  713. mpt3sas_base_free_smid(ioc, smid);
  714. goto out;
  715. }
  716. }
  717. if (data_in_sz) /* READ */ {
  718. data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
  719. &data_in_dma);
  720. if (!data_in) {
  721. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  722. __LINE__, __func__);
  723. ret = -ENOMEM;
  724. mpt3sas_base_free_smid(ioc, smid);
  725. goto out;
  726. }
  727. }
  728. psge = (void *)request + (karg.data_sge_offset*4);
  729. /* send command to firmware */
  730. _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
  731. init_completion(&ioc->ctl_cmds.done);
  732. switch (mpi_request->Function) {
  733. case MPI2_FUNCTION_NVME_ENCAPSULATED:
  734. {
  735. nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
  736. /*
  737. * Get the Physical Address of the sense buffer.
  738. * Use Error Response buffer address field to hold the sense
  739. * buffer address.
  740. * Clear the internal sense buffer, which will potentially hold
  741. * the Completion Queue Entry on return, or 0 if no Entry.
  742. * Build the PRPs and set direction bits.
  743. * Send the request.
  744. */
  745. nvme_encap_request->ErrorResponseBaseAddress =
  746. cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
  747. nvme_encap_request->ErrorResponseBaseAddress |=
  748. cpu_to_le64(le32_to_cpu(
  749. mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
  750. nvme_encap_request->ErrorResponseAllocationLength =
  751. cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
  752. memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
  753. ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
  754. data_out_dma, data_out_sz, data_in_dma, data_in_sz);
  755. if (test_bit(device_handle, ioc->device_remove_in_progress)) {
  756. dtmprintk(ioc, pr_info(MPT3SAS_FMT "handle(0x%04x) :"
  757. "ioctl failed due to device removal in progress\n",
  758. ioc->name, device_handle));
  759. mpt3sas_base_free_smid(ioc, smid);
  760. ret = -EINVAL;
  761. goto out;
  762. }
  763. mpt3sas_base_put_smid_nvme_encap(ioc, smid);
  764. break;
  765. }
  766. case MPI2_FUNCTION_SCSI_IO_REQUEST:
  767. case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  768. {
  769. Mpi2SCSIIORequest_t *scsiio_request =
  770. (Mpi2SCSIIORequest_t *)request;
  771. scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  772. scsiio_request->SenseBufferLowAddress =
  773. mpt3sas_base_get_sense_buffer_dma(ioc, smid);
  774. memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
  775. if (test_bit(device_handle, ioc->device_remove_in_progress)) {
  776. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  777. "handle(0x%04x) :ioctl failed due to device removal in progress\n",
  778. ioc->name, device_handle));
  779. mpt3sas_base_free_smid(ioc, smid);
  780. ret = -EINVAL;
  781. goto out;
  782. }
  783. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
  784. data_in_dma, data_in_sz);
  785. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
  786. ioc->put_smid_scsi_io(ioc, smid, device_handle);
  787. else
  788. mpt3sas_base_put_smid_default(ioc, smid);
  789. break;
  790. }
  791. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  792. {
  793. Mpi2SCSITaskManagementRequest_t *tm_request =
  794. (Mpi2SCSITaskManagementRequest_t *)request;
  795. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  796. "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
  797. ioc->name,
  798. le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
  799. ioc->got_task_abort_from_ioctl = 1;
  800. if (tm_request->TaskType ==
  801. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
  802. tm_request->TaskType ==
  803. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
  804. if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
  805. mpt3sas_base_free_smid(ioc, smid);
  806. ioc->got_task_abort_from_ioctl = 0;
  807. goto out;
  808. }
  809. }
  810. ioc->got_task_abort_from_ioctl = 0;
  811. if (test_bit(device_handle, ioc->device_remove_in_progress)) {
  812. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  813. "handle(0x%04x) :ioctl failed due to device removal in progress\n",
  814. ioc->name, device_handle));
  815. mpt3sas_base_free_smid(ioc, smid);
  816. ret = -EINVAL;
  817. goto out;
  818. }
  819. mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
  820. tm_request->DevHandle));
  821. ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
  822. data_in_dma, data_in_sz);
  823. mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
  824. break;
  825. }
  826. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  827. {
  828. Mpi2SmpPassthroughRequest_t *smp_request =
  829. (Mpi2SmpPassthroughRequest_t *)mpi_request;
  830. u8 *data;
  831. /* ioc determines which port to use */
  832. smp_request->PhysicalPort = 0xFF;
  833. if (smp_request->PassthroughFlags &
  834. MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
  835. data = (u8 *)&smp_request->SGL;
  836. else {
  837. if (unlikely(data_out == NULL)) {
  838. pr_err("failure at %s:%d/%s()!\n",
  839. __FILE__, __LINE__, __func__);
  840. mpt3sas_base_free_smid(ioc, smid);
  841. ret = -EINVAL;
  842. goto out;
  843. }
  844. data = data_out;
  845. }
  846. if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
  847. ioc->ioc_link_reset_in_progress = 1;
  848. ioc->ignore_loginfos = 1;
  849. }
  850. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  851. data_in_sz);
  852. mpt3sas_base_put_smid_default(ioc, smid);
  853. break;
  854. }
  855. case MPI2_FUNCTION_SATA_PASSTHROUGH:
  856. {
  857. if (test_bit(device_handle, ioc->device_remove_in_progress)) {
  858. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  859. "handle(0x%04x) :ioctl failed due to device removal in progress\n",
  860. ioc->name, device_handle));
  861. mpt3sas_base_free_smid(ioc, smid);
  862. ret = -EINVAL;
  863. goto out;
  864. }
  865. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  866. data_in_sz);
  867. mpt3sas_base_put_smid_default(ioc, smid);
  868. break;
  869. }
  870. case MPI2_FUNCTION_FW_DOWNLOAD:
  871. case MPI2_FUNCTION_FW_UPLOAD:
  872. {
  873. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  874. data_in_sz);
  875. mpt3sas_base_put_smid_default(ioc, smid);
  876. break;
  877. }
  878. case MPI2_FUNCTION_TOOLBOX:
  879. {
  880. Mpi2ToolboxCleanRequest_t *toolbox_request =
  881. (Mpi2ToolboxCleanRequest_t *)mpi_request;
  882. if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
  883. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
  884. data_in_dma, data_in_sz);
  885. } else {
  886. ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
  887. data_in_dma, data_in_sz);
  888. }
  889. mpt3sas_base_put_smid_default(ioc, smid);
  890. break;
  891. }
  892. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  893. {
  894. Mpi2SasIoUnitControlRequest_t *sasiounit_request =
  895. (Mpi2SasIoUnitControlRequest_t *)mpi_request;
  896. if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
  897. || sasiounit_request->Operation ==
  898. MPI2_SAS_OP_PHY_LINK_RESET) {
  899. ioc->ioc_link_reset_in_progress = 1;
  900. ioc->ignore_loginfos = 1;
  901. }
  902. /* drop to default case for posting the request */
  903. }
  904. /* fall through */
  905. default:
  906. ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
  907. data_in_dma, data_in_sz);
  908. mpt3sas_base_put_smid_default(ioc, smid);
  909. break;
  910. }
  911. if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
  912. timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
  913. else
  914. timeout = karg.timeout;
  915. wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
  916. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  917. Mpi2SCSITaskManagementRequest_t *tm_request =
  918. (Mpi2SCSITaskManagementRequest_t *)mpi_request;
  919. mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
  920. tm_request->DevHandle));
  921. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
  922. } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
  923. mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
  924. ioc->ioc_link_reset_in_progress) {
  925. ioc->ioc_link_reset_in_progress = 0;
  926. ioc->ignore_loginfos = 0;
  927. }
  928. if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
  929. issue_reset =
  930. mpt3sas_base_check_cmd_timeout(ioc,
  931. ioc->ctl_cmds.status, mpi_request,
  932. karg.data_sge_offset);
  933. goto issue_host_reset;
  934. }
  935. mpi_reply = ioc->ctl_cmds.reply;
  936. if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
  937. (ioc->logging_level & MPT_DEBUG_TM)) {
  938. Mpi2SCSITaskManagementReply_t *tm_reply =
  939. (Mpi2SCSITaskManagementReply_t *)mpi_reply;
  940. pr_info(MPT3SAS_FMT "TASK_MGMT: " \
  941. "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
  942. "TerminationCount(0x%08x)\n", ioc->name,
  943. le16_to_cpu(tm_reply->IOCStatus),
  944. le32_to_cpu(tm_reply->IOCLogInfo),
  945. le32_to_cpu(tm_reply->TerminationCount));
  946. }
  947. /* copy out xdata to user */
  948. if (data_in_sz) {
  949. if (copy_to_user(karg.data_in_buf_ptr, data_in,
  950. data_in_sz)) {
  951. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  952. __LINE__, __func__);
  953. ret = -ENODATA;
  954. goto out;
  955. }
  956. }
  957. /* copy out reply message frame to user */
  958. if (karg.max_reply_bytes) {
  959. sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
  960. if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
  961. sz)) {
  962. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  963. __LINE__, __func__);
  964. ret = -ENODATA;
  965. goto out;
  966. }
  967. }
  968. /* copy out sense/NVMe Error Response to user */
  969. if (karg.max_sense_bytes && (mpi_request->Function ==
  970. MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
  971. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
  972. MPI2_FUNCTION_NVME_ENCAPSULATED)) {
  973. if (karg.sense_data_ptr == NULL) {
  974. pr_info(MPT3SAS_FMT "Response buffer provided"
  975. " by application is NULL; Response data will"
  976. " not be returned.\n", ioc->name);
  977. goto out;
  978. }
  979. sz_arg = (mpi_request->Function ==
  980. MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
  981. SCSI_SENSE_BUFFERSIZE;
  982. sz = min_t(u32, karg.max_sense_bytes, sz_arg);
  983. if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
  984. sz)) {
  985. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  986. __LINE__, __func__);
  987. ret = -ENODATA;
  988. goto out;
  989. }
  990. }
  991. issue_host_reset:
  992. if (issue_reset) {
  993. ret = -ENODATA;
  994. if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  995. mpi_request->Function ==
  996. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  997. mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
  998. pr_info(MPT3SAS_FMT "issue target reset: handle = (0x%04x)\n",
  999. ioc->name,
  1000. le16_to_cpu(mpi_request->FunctionDependent1));
  1001. mpt3sas_halt_firmware(ioc);
  1002. pcie_device = mpt3sas_get_pdev_by_handle(ioc,
  1003. le16_to_cpu(mpi_request->FunctionDependent1));
  1004. if (pcie_device && (!ioc->tm_custom_handling))
  1005. mpt3sas_scsih_issue_locked_tm(ioc,
  1006. le16_to_cpu(mpi_request->FunctionDependent1),
  1007. 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
  1008. 0, pcie_device->reset_timeout,
  1009. tr_method);
  1010. else
  1011. mpt3sas_scsih_issue_locked_tm(ioc,
  1012. le16_to_cpu(mpi_request->FunctionDependent1),
  1013. 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
  1014. 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
  1015. } else
  1016. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1017. }
  1018. out:
  1019. if (pcie_device)
  1020. pcie_device_put(pcie_device);
  1021. /* free memory associated with sg buffers */
  1022. if (data_in)
  1023. pci_free_consistent(ioc->pdev, data_in_sz, data_in,
  1024. data_in_dma);
  1025. if (data_out)
  1026. pci_free_consistent(ioc->pdev, data_out_sz, data_out,
  1027. data_out_dma);
  1028. kfree(mpi_request);
  1029. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  1030. return ret;
  1031. }
  1032. /**
  1033. * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
  1034. * @ioc: per adapter object
  1035. * @arg: user space buffer containing ioctl content
  1036. */
  1037. static long
  1038. _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1039. {
  1040. struct mpt3_ioctl_iocinfo karg;
  1041. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1042. __func__));
  1043. memset(&karg, 0 , sizeof(karg));
  1044. if (ioc->pfacts)
  1045. karg.port_number = ioc->pfacts[0].PortNumber;
  1046. karg.hw_rev = ioc->pdev->revision;
  1047. karg.pci_id = ioc->pdev->device;
  1048. karg.subsystem_device = ioc->pdev->subsystem_device;
  1049. karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
  1050. karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
  1051. karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
  1052. karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
  1053. karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
  1054. karg.firmware_version = ioc->facts.FWVersion.Word;
  1055. strcpy(karg.driver_version, ioc->driver_name);
  1056. strcat(karg.driver_version, "-");
  1057. switch (ioc->hba_mpi_version_belonged) {
  1058. case MPI2_VERSION:
  1059. if (ioc->is_warpdrive)
  1060. karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
  1061. else
  1062. karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
  1063. strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
  1064. break;
  1065. case MPI25_VERSION:
  1066. case MPI26_VERSION:
  1067. if (ioc->is_gen35_ioc)
  1068. karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
  1069. else
  1070. karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
  1071. strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
  1072. break;
  1073. }
  1074. karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  1075. if (copy_to_user(arg, &karg, sizeof(karg))) {
  1076. pr_err("failure at %s:%d/%s()!\n",
  1077. __FILE__, __LINE__, __func__);
  1078. return -EFAULT;
  1079. }
  1080. return 0;
  1081. }
  1082. /**
  1083. * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
  1084. * @ioc: per adapter object
  1085. * @arg: user space buffer containing ioctl content
  1086. */
  1087. static long
  1088. _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1089. {
  1090. struct mpt3_ioctl_eventquery karg;
  1091. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1092. pr_err("failure at %s:%d/%s()!\n",
  1093. __FILE__, __LINE__, __func__);
  1094. return -EFAULT;
  1095. }
  1096. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1097. __func__));
  1098. karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
  1099. memcpy(karg.event_types, ioc->event_type,
  1100. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
  1101. if (copy_to_user(arg, &karg, sizeof(karg))) {
  1102. pr_err("failure at %s:%d/%s()!\n",
  1103. __FILE__, __LINE__, __func__);
  1104. return -EFAULT;
  1105. }
  1106. return 0;
  1107. }
  1108. /**
  1109. * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
  1110. * @ioc: per adapter object
  1111. * @arg: user space buffer containing ioctl content
  1112. */
  1113. static long
  1114. _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1115. {
  1116. struct mpt3_ioctl_eventenable karg;
  1117. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1118. pr_err("failure at %s:%d/%s()!\n",
  1119. __FILE__, __LINE__, __func__);
  1120. return -EFAULT;
  1121. }
  1122. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1123. __func__));
  1124. memcpy(ioc->event_type, karg.event_types,
  1125. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
  1126. mpt3sas_base_validate_event_type(ioc, ioc->event_type);
  1127. if (ioc->event_log)
  1128. return 0;
  1129. /* initialize event_log */
  1130. ioc->event_context = 0;
  1131. ioc->aen_event_read_flag = 0;
  1132. ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
  1133. sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
  1134. if (!ioc->event_log) {
  1135. pr_err("failure at %s:%d/%s()!\n",
  1136. __FILE__, __LINE__, __func__);
  1137. return -ENOMEM;
  1138. }
  1139. return 0;
  1140. }
  1141. /**
  1142. * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
  1143. * @ioc: per adapter object
  1144. * @arg: user space buffer containing ioctl content
  1145. */
  1146. static long
  1147. _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1148. {
  1149. struct mpt3_ioctl_eventreport karg;
  1150. u32 number_bytes, max_events, max;
  1151. struct mpt3_ioctl_eventreport __user *uarg = arg;
  1152. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1153. pr_err("failure at %s:%d/%s()!\n",
  1154. __FILE__, __LINE__, __func__);
  1155. return -EFAULT;
  1156. }
  1157. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1158. __func__));
  1159. number_bytes = karg.hdr.max_data_size -
  1160. sizeof(struct mpt3_ioctl_header);
  1161. max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
  1162. max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
  1163. /* If fewer than 1 event is requested, there must have
  1164. * been some type of error.
  1165. */
  1166. if (!max || !ioc->event_log)
  1167. return -ENODATA;
  1168. number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
  1169. if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
  1170. pr_err("failure at %s:%d/%s()!\n",
  1171. __FILE__, __LINE__, __func__);
  1172. return -EFAULT;
  1173. }
  1174. /* reset flag so SIGIO can restart */
  1175. ioc->aen_event_read_flag = 0;
  1176. return 0;
  1177. }
  1178. /**
  1179. * _ctl_do_reset - main handler for MPT3HARDRESET opcode
  1180. * @ioc: per adapter object
  1181. * @arg: user space buffer containing ioctl content
  1182. */
  1183. static long
  1184. _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1185. {
  1186. struct mpt3_ioctl_diag_reset karg;
  1187. int retval;
  1188. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1189. pr_err("failure at %s:%d/%s()!\n",
  1190. __FILE__, __LINE__, __func__);
  1191. return -EFAULT;
  1192. }
  1193. if (ioc->shost_recovery || ioc->pci_error_recovery ||
  1194. ioc->is_driver_loading)
  1195. return -EAGAIN;
  1196. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  1197. __func__));
  1198. retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1199. pr_info(MPT3SAS_FMT "host reset: %s\n",
  1200. ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
  1201. return 0;
  1202. }
  1203. /**
  1204. * _ctl_btdh_search_sas_device - searching for sas device
  1205. * @ioc: per adapter object
  1206. * @btdh: btdh ioctl payload
  1207. */
  1208. static int
  1209. _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
  1210. struct mpt3_ioctl_btdh_mapping *btdh)
  1211. {
  1212. struct _sas_device *sas_device;
  1213. unsigned long flags;
  1214. int rc = 0;
  1215. if (list_empty(&ioc->sas_device_list))
  1216. return rc;
  1217. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1218. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  1219. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1220. btdh->handle == sas_device->handle) {
  1221. btdh->bus = sas_device->channel;
  1222. btdh->id = sas_device->id;
  1223. rc = 1;
  1224. goto out;
  1225. } else if (btdh->bus == sas_device->channel && btdh->id ==
  1226. sas_device->id && btdh->handle == 0xFFFF) {
  1227. btdh->handle = sas_device->handle;
  1228. rc = 1;
  1229. goto out;
  1230. }
  1231. }
  1232. out:
  1233. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1234. return rc;
  1235. }
  1236. /**
  1237. * _ctl_btdh_search_pcie_device - searching for pcie device
  1238. * @ioc: per adapter object
  1239. * @btdh: btdh ioctl payload
  1240. */
  1241. static int
  1242. _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
  1243. struct mpt3_ioctl_btdh_mapping *btdh)
  1244. {
  1245. struct _pcie_device *pcie_device;
  1246. unsigned long flags;
  1247. int rc = 0;
  1248. if (list_empty(&ioc->pcie_device_list))
  1249. return rc;
  1250. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  1251. list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
  1252. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1253. btdh->handle == pcie_device->handle) {
  1254. btdh->bus = pcie_device->channel;
  1255. btdh->id = pcie_device->id;
  1256. rc = 1;
  1257. goto out;
  1258. } else if (btdh->bus == pcie_device->channel && btdh->id ==
  1259. pcie_device->id && btdh->handle == 0xFFFF) {
  1260. btdh->handle = pcie_device->handle;
  1261. rc = 1;
  1262. goto out;
  1263. }
  1264. }
  1265. out:
  1266. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  1267. return rc;
  1268. }
  1269. /**
  1270. * _ctl_btdh_search_raid_device - searching for raid device
  1271. * @ioc: per adapter object
  1272. * @btdh: btdh ioctl payload
  1273. */
  1274. static int
  1275. _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
  1276. struct mpt3_ioctl_btdh_mapping *btdh)
  1277. {
  1278. struct _raid_device *raid_device;
  1279. unsigned long flags;
  1280. int rc = 0;
  1281. if (list_empty(&ioc->raid_device_list))
  1282. return rc;
  1283. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1284. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  1285. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1286. btdh->handle == raid_device->handle) {
  1287. btdh->bus = raid_device->channel;
  1288. btdh->id = raid_device->id;
  1289. rc = 1;
  1290. goto out;
  1291. } else if (btdh->bus == raid_device->channel && btdh->id ==
  1292. raid_device->id && btdh->handle == 0xFFFF) {
  1293. btdh->handle = raid_device->handle;
  1294. rc = 1;
  1295. goto out;
  1296. }
  1297. }
  1298. out:
  1299. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1300. return rc;
  1301. }
  1302. /**
  1303. * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
  1304. * @ioc: per adapter object
  1305. * @arg: user space buffer containing ioctl content
  1306. */
  1307. static long
  1308. _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1309. {
  1310. struct mpt3_ioctl_btdh_mapping karg;
  1311. int rc;
  1312. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1313. pr_err("failure at %s:%d/%s()!\n",
  1314. __FILE__, __LINE__, __func__);
  1315. return -EFAULT;
  1316. }
  1317. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1318. __func__));
  1319. rc = _ctl_btdh_search_sas_device(ioc, &karg);
  1320. if (!rc)
  1321. rc = _ctl_btdh_search_pcie_device(ioc, &karg);
  1322. if (!rc)
  1323. _ctl_btdh_search_raid_device(ioc, &karg);
  1324. if (copy_to_user(arg, &karg, sizeof(karg))) {
  1325. pr_err("failure at %s:%d/%s()!\n",
  1326. __FILE__, __LINE__, __func__);
  1327. return -EFAULT;
  1328. }
  1329. return 0;
  1330. }
  1331. /**
  1332. * _ctl_diag_capability - return diag buffer capability
  1333. * @ioc: per adapter object
  1334. * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
  1335. *
  1336. * returns 1 when diag buffer support is enabled in firmware
  1337. */
  1338. static u8
  1339. _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
  1340. {
  1341. u8 rc = 0;
  1342. switch (buffer_type) {
  1343. case MPI2_DIAG_BUF_TYPE_TRACE:
  1344. if (ioc->facts.IOCCapabilities &
  1345. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
  1346. rc = 1;
  1347. break;
  1348. case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
  1349. if (ioc->facts.IOCCapabilities &
  1350. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
  1351. rc = 1;
  1352. break;
  1353. case MPI2_DIAG_BUF_TYPE_EXTENDED:
  1354. if (ioc->facts.IOCCapabilities &
  1355. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
  1356. rc = 1;
  1357. }
  1358. return rc;
  1359. }
  1360. /**
  1361. * _ctl_diag_register_2 - wrapper for registering diag buffer support
  1362. * @ioc: per adapter object
  1363. * @diag_register: the diag_register struct passed in from user space
  1364. *
  1365. */
  1366. static long
  1367. _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
  1368. struct mpt3_diag_register *diag_register)
  1369. {
  1370. int rc, i;
  1371. void *request_data = NULL;
  1372. dma_addr_t request_data_dma;
  1373. u32 request_data_sz = 0;
  1374. Mpi2DiagBufferPostRequest_t *mpi_request;
  1375. Mpi2DiagBufferPostReply_t *mpi_reply;
  1376. u8 buffer_type;
  1377. u16 smid;
  1378. u16 ioc_status;
  1379. u32 ioc_state;
  1380. u8 issue_reset = 0;
  1381. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1382. __func__));
  1383. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1384. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1385. pr_err(MPT3SAS_FMT
  1386. "%s: failed due to ioc not operational\n",
  1387. ioc->name, __func__);
  1388. rc = -EAGAIN;
  1389. goto out;
  1390. }
  1391. if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
  1392. pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
  1393. ioc->name, __func__);
  1394. rc = -EAGAIN;
  1395. goto out;
  1396. }
  1397. buffer_type = diag_register->buffer_type;
  1398. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1399. pr_err(MPT3SAS_FMT
  1400. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1401. ioc->name, __func__, buffer_type);
  1402. return -EPERM;
  1403. }
  1404. if (ioc->diag_buffer_status[buffer_type] &
  1405. MPT3_DIAG_BUFFER_IS_REGISTERED) {
  1406. pr_err(MPT3SAS_FMT
  1407. "%s: already has a registered buffer for buffer_type(0x%02x)\n",
  1408. ioc->name, __func__,
  1409. buffer_type);
  1410. return -EINVAL;
  1411. }
  1412. if (diag_register->requested_buffer_size % 4) {
  1413. pr_err(MPT3SAS_FMT
  1414. "%s: the requested_buffer_size is not 4 byte aligned\n",
  1415. ioc->name, __func__);
  1416. return -EINVAL;
  1417. }
  1418. smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1419. if (!smid) {
  1420. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1421. ioc->name, __func__);
  1422. rc = -EAGAIN;
  1423. goto out;
  1424. }
  1425. rc = 0;
  1426. ioc->ctl_cmds.status = MPT3_CMD_PENDING;
  1427. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1428. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1429. ioc->ctl_cmds.smid = smid;
  1430. request_data = ioc->diag_buffer[buffer_type];
  1431. request_data_sz = diag_register->requested_buffer_size;
  1432. ioc->unique_id[buffer_type] = diag_register->unique_id;
  1433. ioc->diag_buffer_status[buffer_type] = 0;
  1434. memcpy(ioc->product_specific[buffer_type],
  1435. diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
  1436. ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
  1437. if (request_data) {
  1438. request_data_dma = ioc->diag_buffer_dma[buffer_type];
  1439. if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
  1440. pci_free_consistent(ioc->pdev,
  1441. ioc->diag_buffer_sz[buffer_type],
  1442. request_data, request_data_dma);
  1443. request_data = NULL;
  1444. }
  1445. }
  1446. if (request_data == NULL) {
  1447. ioc->diag_buffer_sz[buffer_type] = 0;
  1448. ioc->diag_buffer_dma[buffer_type] = 0;
  1449. request_data = pci_alloc_consistent(
  1450. ioc->pdev, request_data_sz, &request_data_dma);
  1451. if (request_data == NULL) {
  1452. pr_err(MPT3SAS_FMT "%s: failed allocating memory" \
  1453. " for diag buffers, requested size(%d)\n",
  1454. ioc->name, __func__, request_data_sz);
  1455. mpt3sas_base_free_smid(ioc, smid);
  1456. rc = -ENOMEM;
  1457. goto out;
  1458. }
  1459. ioc->diag_buffer[buffer_type] = request_data;
  1460. ioc->diag_buffer_sz[buffer_type] = request_data_sz;
  1461. ioc->diag_buffer_dma[buffer_type] = request_data_dma;
  1462. }
  1463. mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
  1464. mpi_request->BufferType = diag_register->buffer_type;
  1465. mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
  1466. mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
  1467. mpi_request->BufferLength = cpu_to_le32(request_data_sz);
  1468. mpi_request->VF_ID = 0; /* TODO */
  1469. mpi_request->VP_ID = 0;
  1470. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1471. "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
  1472. ioc->name, __func__, request_data,
  1473. (unsigned long long)request_data_dma,
  1474. le32_to_cpu(mpi_request->BufferLength)));
  1475. for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
  1476. mpi_request->ProductSpecific[i] =
  1477. cpu_to_le32(ioc->product_specific[buffer_type][i]);
  1478. init_completion(&ioc->ctl_cmds.done);
  1479. mpt3sas_base_put_smid_default(ioc, smid);
  1480. wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1481. MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
  1482. if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
  1483. issue_reset =
  1484. mpt3sas_base_check_cmd_timeout(ioc,
  1485. ioc->ctl_cmds.status, mpi_request,
  1486. sizeof(Mpi2DiagBufferPostRequest_t)/4);
  1487. goto issue_host_reset;
  1488. }
  1489. /* process the completed Reply Message Frame */
  1490. if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
  1491. pr_err(MPT3SAS_FMT "%s: no reply message\n",
  1492. ioc->name, __func__);
  1493. rc = -EFAULT;
  1494. goto out;
  1495. }
  1496. mpi_reply = ioc->ctl_cmds.reply;
  1497. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1498. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1499. ioc->diag_buffer_status[buffer_type] |=
  1500. MPT3_DIAG_BUFFER_IS_REGISTERED;
  1501. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
  1502. ioc->name, __func__));
  1503. } else {
  1504. pr_info(MPT3SAS_FMT
  1505. "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
  1506. ioc->name, __func__,
  1507. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1508. rc = -EFAULT;
  1509. }
  1510. issue_host_reset:
  1511. if (issue_reset)
  1512. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1513. out:
  1514. if (rc && request_data)
  1515. pci_free_consistent(ioc->pdev, request_data_sz,
  1516. request_data, request_data_dma);
  1517. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  1518. return rc;
  1519. }
  1520. /**
  1521. * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
  1522. * @ioc: per adapter object
  1523. * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
  1524. *
  1525. * This is called when command line option diag_buffer_enable is enabled
  1526. * at driver load time.
  1527. */
  1528. void
  1529. mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
  1530. {
  1531. struct mpt3_diag_register diag_register;
  1532. memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
  1533. if (bits_to_register & 1) {
  1534. pr_info(MPT3SAS_FMT "registering trace buffer support\n",
  1535. ioc->name);
  1536. ioc->diag_trigger_master.MasterData =
  1537. (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
  1538. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  1539. /* register for 2MB buffers */
  1540. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1541. diag_register.unique_id = 0x7075900;
  1542. _ctl_diag_register_2(ioc, &diag_register);
  1543. }
  1544. if (bits_to_register & 2) {
  1545. pr_info(MPT3SAS_FMT "registering snapshot buffer support\n",
  1546. ioc->name);
  1547. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
  1548. /* register for 2MB buffers */
  1549. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1550. diag_register.unique_id = 0x7075901;
  1551. _ctl_diag_register_2(ioc, &diag_register);
  1552. }
  1553. if (bits_to_register & 4) {
  1554. pr_info(MPT3SAS_FMT "registering extended buffer support\n",
  1555. ioc->name);
  1556. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
  1557. /* register for 2MB buffers */
  1558. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1559. diag_register.unique_id = 0x7075901;
  1560. _ctl_diag_register_2(ioc, &diag_register);
  1561. }
  1562. }
  1563. /**
  1564. * _ctl_diag_register - application register with driver
  1565. * @ioc: per adapter object
  1566. * @arg: user space buffer containing ioctl content
  1567. *
  1568. * This will allow the driver to setup any required buffers that will be
  1569. * needed by firmware to communicate with the driver.
  1570. */
  1571. static long
  1572. _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1573. {
  1574. struct mpt3_diag_register karg;
  1575. long rc;
  1576. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1577. pr_err("failure at %s:%d/%s()!\n",
  1578. __FILE__, __LINE__, __func__);
  1579. return -EFAULT;
  1580. }
  1581. rc = _ctl_diag_register_2(ioc, &karg);
  1582. return rc;
  1583. }
  1584. /**
  1585. * _ctl_diag_unregister - application unregister with driver
  1586. * @ioc: per adapter object
  1587. * @arg: user space buffer containing ioctl content
  1588. *
  1589. * This will allow the driver to cleanup any memory allocated for diag
  1590. * messages and to free up any resources.
  1591. */
  1592. static long
  1593. _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1594. {
  1595. struct mpt3_diag_unregister karg;
  1596. void *request_data;
  1597. dma_addr_t request_data_dma;
  1598. u32 request_data_sz;
  1599. u8 buffer_type;
  1600. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1601. pr_err("failure at %s:%d/%s()!\n",
  1602. __FILE__, __LINE__, __func__);
  1603. return -EFAULT;
  1604. }
  1605. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1606. __func__));
  1607. buffer_type = karg.unique_id & 0x000000ff;
  1608. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1609. pr_err(MPT3SAS_FMT
  1610. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1611. ioc->name, __func__, buffer_type);
  1612. return -EPERM;
  1613. }
  1614. if ((ioc->diag_buffer_status[buffer_type] &
  1615. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1616. pr_err(MPT3SAS_FMT
  1617. "%s: buffer_type(0x%02x) is not registered\n",
  1618. ioc->name, __func__, buffer_type);
  1619. return -EINVAL;
  1620. }
  1621. if ((ioc->diag_buffer_status[buffer_type] &
  1622. MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
  1623. pr_err(MPT3SAS_FMT
  1624. "%s: buffer_type(0x%02x) has not been released\n",
  1625. ioc->name, __func__, buffer_type);
  1626. return -EINVAL;
  1627. }
  1628. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1629. pr_err(MPT3SAS_FMT
  1630. "%s: unique_id(0x%08x) is not registered\n",
  1631. ioc->name, __func__, karg.unique_id);
  1632. return -EINVAL;
  1633. }
  1634. request_data = ioc->diag_buffer[buffer_type];
  1635. if (!request_data) {
  1636. pr_err(MPT3SAS_FMT
  1637. "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
  1638. ioc->name, __func__, buffer_type);
  1639. return -ENOMEM;
  1640. }
  1641. request_data_sz = ioc->diag_buffer_sz[buffer_type];
  1642. request_data_dma = ioc->diag_buffer_dma[buffer_type];
  1643. pci_free_consistent(ioc->pdev, request_data_sz,
  1644. request_data, request_data_dma);
  1645. ioc->diag_buffer[buffer_type] = NULL;
  1646. ioc->diag_buffer_status[buffer_type] = 0;
  1647. return 0;
  1648. }
  1649. /**
  1650. * _ctl_diag_query - query relevant info associated with diag buffers
  1651. * @ioc: per adapter object
  1652. * @arg: user space buffer containing ioctl content
  1653. *
  1654. * The application will send only buffer_type and unique_id. Driver will
  1655. * inspect unique_id first, if valid, fill in all the info. If unique_id is
  1656. * 0x00, the driver will return info specified by Buffer Type.
  1657. */
  1658. static long
  1659. _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1660. {
  1661. struct mpt3_diag_query karg;
  1662. void *request_data;
  1663. int i;
  1664. u8 buffer_type;
  1665. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1666. pr_err("failure at %s:%d/%s()!\n",
  1667. __FILE__, __LINE__, __func__);
  1668. return -EFAULT;
  1669. }
  1670. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1671. __func__));
  1672. karg.application_flags = 0;
  1673. buffer_type = karg.buffer_type;
  1674. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1675. pr_err(MPT3SAS_FMT
  1676. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1677. ioc->name, __func__, buffer_type);
  1678. return -EPERM;
  1679. }
  1680. if ((ioc->diag_buffer_status[buffer_type] &
  1681. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1682. pr_err(MPT3SAS_FMT
  1683. "%s: buffer_type(0x%02x) is not registered\n",
  1684. ioc->name, __func__, buffer_type);
  1685. return -EINVAL;
  1686. }
  1687. if (karg.unique_id & 0xffffff00) {
  1688. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1689. pr_err(MPT3SAS_FMT
  1690. "%s: unique_id(0x%08x) is not registered\n",
  1691. ioc->name, __func__, karg.unique_id);
  1692. return -EINVAL;
  1693. }
  1694. }
  1695. request_data = ioc->diag_buffer[buffer_type];
  1696. if (!request_data) {
  1697. pr_err(MPT3SAS_FMT
  1698. "%s: doesn't have buffer for buffer_type(0x%02x)\n",
  1699. ioc->name, __func__, buffer_type);
  1700. return -ENOMEM;
  1701. }
  1702. if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
  1703. karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
  1704. MPT3_APP_FLAGS_BUFFER_VALID);
  1705. else
  1706. karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
  1707. MPT3_APP_FLAGS_BUFFER_VALID |
  1708. MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
  1709. for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
  1710. karg.product_specific[i] =
  1711. ioc->product_specific[buffer_type][i];
  1712. karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
  1713. karg.driver_added_buffer_size = 0;
  1714. karg.unique_id = ioc->unique_id[buffer_type];
  1715. karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
  1716. if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
  1717. pr_err(MPT3SAS_FMT
  1718. "%s: unable to write mpt3_diag_query data @ %p\n",
  1719. ioc->name, __func__, arg);
  1720. return -EFAULT;
  1721. }
  1722. return 0;
  1723. }
  1724. /**
  1725. * mpt3sas_send_diag_release - Diag Release Message
  1726. * @ioc: per adapter object
  1727. * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
  1728. * @issue_reset: specifies whether host reset is required.
  1729. *
  1730. */
  1731. int
  1732. mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
  1733. u8 *issue_reset)
  1734. {
  1735. Mpi2DiagReleaseRequest_t *mpi_request;
  1736. Mpi2DiagReleaseReply_t *mpi_reply;
  1737. u16 smid;
  1738. u16 ioc_status;
  1739. u32 ioc_state;
  1740. int rc;
  1741. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1742. __func__));
  1743. rc = 0;
  1744. *issue_reset = 0;
  1745. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1746. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1747. if (ioc->diag_buffer_status[buffer_type] &
  1748. MPT3_DIAG_BUFFER_IS_REGISTERED)
  1749. ioc->diag_buffer_status[buffer_type] |=
  1750. MPT3_DIAG_BUFFER_IS_RELEASED;
  1751. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1752. "%s: skipping due to FAULT state\n", ioc->name,
  1753. __func__));
  1754. rc = -EAGAIN;
  1755. goto out;
  1756. }
  1757. if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
  1758. pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
  1759. ioc->name, __func__);
  1760. rc = -EAGAIN;
  1761. goto out;
  1762. }
  1763. smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1764. if (!smid) {
  1765. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1766. ioc->name, __func__);
  1767. rc = -EAGAIN;
  1768. goto out;
  1769. }
  1770. ioc->ctl_cmds.status = MPT3_CMD_PENDING;
  1771. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1772. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1773. ioc->ctl_cmds.smid = smid;
  1774. mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
  1775. mpi_request->BufferType = buffer_type;
  1776. mpi_request->VF_ID = 0; /* TODO */
  1777. mpi_request->VP_ID = 0;
  1778. init_completion(&ioc->ctl_cmds.done);
  1779. mpt3sas_base_put_smid_default(ioc, smid);
  1780. wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1781. MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
  1782. if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
  1783. *issue_reset = mpt3sas_base_check_cmd_timeout(ioc,
  1784. ioc->ctl_cmds.status, mpi_request,
  1785. sizeof(Mpi2DiagReleaseRequest_t)/4);
  1786. rc = -EFAULT;
  1787. goto out;
  1788. }
  1789. /* process the completed Reply Message Frame */
  1790. if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
  1791. pr_err(MPT3SAS_FMT "%s: no reply message\n",
  1792. ioc->name, __func__);
  1793. rc = -EFAULT;
  1794. goto out;
  1795. }
  1796. mpi_reply = ioc->ctl_cmds.reply;
  1797. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1798. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1799. ioc->diag_buffer_status[buffer_type] |=
  1800. MPT3_DIAG_BUFFER_IS_RELEASED;
  1801. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
  1802. ioc->name, __func__));
  1803. } else {
  1804. pr_info(MPT3SAS_FMT
  1805. "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
  1806. ioc->name, __func__,
  1807. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1808. rc = -EFAULT;
  1809. }
  1810. out:
  1811. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  1812. return rc;
  1813. }
  1814. /**
  1815. * _ctl_diag_release - request to send Diag Release Message to firmware
  1816. * @ioc: ?
  1817. * @arg: user space buffer containing ioctl content
  1818. *
  1819. * This allows ownership of the specified buffer to returned to the driver,
  1820. * allowing an application to read the buffer without fear that firmware is
  1821. * overwriting information in the buffer.
  1822. */
  1823. static long
  1824. _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1825. {
  1826. struct mpt3_diag_release karg;
  1827. void *request_data;
  1828. int rc;
  1829. u8 buffer_type;
  1830. u8 issue_reset = 0;
  1831. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1832. pr_err("failure at %s:%d/%s()!\n",
  1833. __FILE__, __LINE__, __func__);
  1834. return -EFAULT;
  1835. }
  1836. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1837. __func__));
  1838. buffer_type = karg.unique_id & 0x000000ff;
  1839. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1840. pr_err(MPT3SAS_FMT
  1841. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1842. ioc->name, __func__, buffer_type);
  1843. return -EPERM;
  1844. }
  1845. if ((ioc->diag_buffer_status[buffer_type] &
  1846. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1847. pr_err(MPT3SAS_FMT
  1848. "%s: buffer_type(0x%02x) is not registered\n",
  1849. ioc->name, __func__, buffer_type);
  1850. return -EINVAL;
  1851. }
  1852. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1853. pr_err(MPT3SAS_FMT
  1854. "%s: unique_id(0x%08x) is not registered\n",
  1855. ioc->name, __func__, karg.unique_id);
  1856. return -EINVAL;
  1857. }
  1858. if (ioc->diag_buffer_status[buffer_type] &
  1859. MPT3_DIAG_BUFFER_IS_RELEASED) {
  1860. pr_err(MPT3SAS_FMT
  1861. "%s: buffer_type(0x%02x) is already released\n",
  1862. ioc->name, __func__,
  1863. buffer_type);
  1864. return 0;
  1865. }
  1866. request_data = ioc->diag_buffer[buffer_type];
  1867. if (!request_data) {
  1868. pr_err(MPT3SAS_FMT
  1869. "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
  1870. ioc->name, __func__, buffer_type);
  1871. return -ENOMEM;
  1872. }
  1873. /* buffers were released by due to host reset */
  1874. if ((ioc->diag_buffer_status[buffer_type] &
  1875. MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
  1876. ioc->diag_buffer_status[buffer_type] |=
  1877. MPT3_DIAG_BUFFER_IS_RELEASED;
  1878. ioc->diag_buffer_status[buffer_type] &=
  1879. ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
  1880. pr_err(MPT3SAS_FMT
  1881. "%s: buffer_type(0x%02x) was released due to host reset\n",
  1882. ioc->name, __func__, buffer_type);
  1883. return 0;
  1884. }
  1885. rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
  1886. if (issue_reset)
  1887. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1888. return rc;
  1889. }
  1890. /**
  1891. * _ctl_diag_read_buffer - request for copy of the diag buffer
  1892. * @ioc: per adapter object
  1893. * @arg: user space buffer containing ioctl content
  1894. */
  1895. static long
  1896. _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
  1897. {
  1898. struct mpt3_diag_read_buffer karg;
  1899. struct mpt3_diag_read_buffer __user *uarg = arg;
  1900. void *request_data, *diag_data;
  1901. Mpi2DiagBufferPostRequest_t *mpi_request;
  1902. Mpi2DiagBufferPostReply_t *mpi_reply;
  1903. int rc, i;
  1904. u8 buffer_type;
  1905. unsigned long request_size, copy_size;
  1906. u16 smid;
  1907. u16 ioc_status;
  1908. u8 issue_reset = 0;
  1909. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1910. pr_err("failure at %s:%d/%s()!\n",
  1911. __FILE__, __LINE__, __func__);
  1912. return -EFAULT;
  1913. }
  1914. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  1915. __func__));
  1916. buffer_type = karg.unique_id & 0x000000ff;
  1917. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1918. pr_err(MPT3SAS_FMT
  1919. "%s: doesn't have capability for buffer_type(0x%02x)\n",
  1920. ioc->name, __func__, buffer_type);
  1921. return -EPERM;
  1922. }
  1923. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1924. pr_err(MPT3SAS_FMT
  1925. "%s: unique_id(0x%08x) is not registered\n",
  1926. ioc->name, __func__, karg.unique_id);
  1927. return -EINVAL;
  1928. }
  1929. request_data = ioc->diag_buffer[buffer_type];
  1930. if (!request_data) {
  1931. pr_err(MPT3SAS_FMT
  1932. "%s: doesn't have buffer for buffer_type(0x%02x)\n",
  1933. ioc->name, __func__, buffer_type);
  1934. return -ENOMEM;
  1935. }
  1936. request_size = ioc->diag_buffer_sz[buffer_type];
  1937. if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
  1938. pr_err(MPT3SAS_FMT "%s: either the starting_offset " \
  1939. "or bytes_to_read are not 4 byte aligned\n", ioc->name,
  1940. __func__);
  1941. return -EINVAL;
  1942. }
  1943. if (karg.starting_offset > request_size)
  1944. return -EINVAL;
  1945. diag_data = (void *)(request_data + karg.starting_offset);
  1946. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1947. "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
  1948. ioc->name, __func__,
  1949. diag_data, karg.starting_offset, karg.bytes_to_read));
  1950. /* Truncate data on requests that are too large */
  1951. if ((diag_data + karg.bytes_to_read < diag_data) ||
  1952. (diag_data + karg.bytes_to_read > request_data + request_size))
  1953. copy_size = request_size - karg.starting_offset;
  1954. else
  1955. copy_size = karg.bytes_to_read;
  1956. if (copy_to_user((void __user *)uarg->diagnostic_data,
  1957. diag_data, copy_size)) {
  1958. pr_err(MPT3SAS_FMT
  1959. "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
  1960. ioc->name, __func__, diag_data);
  1961. return -EFAULT;
  1962. }
  1963. if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
  1964. return 0;
  1965. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1966. "%s: Reregister buffer_type(0x%02x)\n",
  1967. ioc->name, __func__, buffer_type));
  1968. if ((ioc->diag_buffer_status[buffer_type] &
  1969. MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
  1970. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  1971. "%s: buffer_type(0x%02x) is still registered\n",
  1972. ioc->name, __func__, buffer_type));
  1973. return 0;
  1974. }
  1975. /* Get a free request frame and save the message context.
  1976. */
  1977. if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
  1978. pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
  1979. ioc->name, __func__);
  1980. rc = -EAGAIN;
  1981. goto out;
  1982. }
  1983. smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1984. if (!smid) {
  1985. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1986. ioc->name, __func__);
  1987. rc = -EAGAIN;
  1988. goto out;
  1989. }
  1990. rc = 0;
  1991. ioc->ctl_cmds.status = MPT3_CMD_PENDING;
  1992. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1993. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1994. ioc->ctl_cmds.smid = smid;
  1995. mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
  1996. mpi_request->BufferType = buffer_type;
  1997. mpi_request->BufferLength =
  1998. cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
  1999. mpi_request->BufferAddress =
  2000. cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
  2001. for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
  2002. mpi_request->ProductSpecific[i] =
  2003. cpu_to_le32(ioc->product_specific[buffer_type][i]);
  2004. mpi_request->VF_ID = 0; /* TODO */
  2005. mpi_request->VP_ID = 0;
  2006. init_completion(&ioc->ctl_cmds.done);
  2007. mpt3sas_base_put_smid_default(ioc, smid);
  2008. wait_for_completion_timeout(&ioc->ctl_cmds.done,
  2009. MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
  2010. if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
  2011. issue_reset =
  2012. mpt3sas_base_check_cmd_timeout(ioc,
  2013. ioc->ctl_cmds.status, mpi_request,
  2014. sizeof(Mpi2DiagBufferPostRequest_t)/4);
  2015. goto issue_host_reset;
  2016. }
  2017. /* process the completed Reply Message Frame */
  2018. if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
  2019. pr_err(MPT3SAS_FMT "%s: no reply message\n",
  2020. ioc->name, __func__);
  2021. rc = -EFAULT;
  2022. goto out;
  2023. }
  2024. mpi_reply = ioc->ctl_cmds.reply;
  2025. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  2026. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  2027. ioc->diag_buffer_status[buffer_type] |=
  2028. MPT3_DIAG_BUFFER_IS_REGISTERED;
  2029. dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
  2030. ioc->name, __func__));
  2031. } else {
  2032. pr_info(MPT3SAS_FMT
  2033. "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
  2034. ioc->name, __func__,
  2035. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  2036. rc = -EFAULT;
  2037. }
  2038. issue_host_reset:
  2039. if (issue_reset)
  2040. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  2041. out:
  2042. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  2043. return rc;
  2044. }
  2045. #ifdef CONFIG_COMPAT
  2046. /**
  2047. * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
  2048. * @ioc: per adapter object
  2049. * @cmd: ioctl opcode
  2050. * @arg: (struct mpt3_ioctl_command32)
  2051. *
  2052. * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
  2053. */
  2054. static long
  2055. _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
  2056. void __user *arg)
  2057. {
  2058. struct mpt3_ioctl_command32 karg32;
  2059. struct mpt3_ioctl_command32 __user *uarg;
  2060. struct mpt3_ioctl_command karg;
  2061. if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
  2062. return -EINVAL;
  2063. uarg = (struct mpt3_ioctl_command32 __user *) arg;
  2064. if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
  2065. pr_err("failure at %s:%d/%s()!\n",
  2066. __FILE__, __LINE__, __func__);
  2067. return -EFAULT;
  2068. }
  2069. memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
  2070. karg.hdr.ioc_number = karg32.hdr.ioc_number;
  2071. karg.hdr.port_number = karg32.hdr.port_number;
  2072. karg.hdr.max_data_size = karg32.hdr.max_data_size;
  2073. karg.timeout = karg32.timeout;
  2074. karg.max_reply_bytes = karg32.max_reply_bytes;
  2075. karg.data_in_size = karg32.data_in_size;
  2076. karg.data_out_size = karg32.data_out_size;
  2077. karg.max_sense_bytes = karg32.max_sense_bytes;
  2078. karg.data_sge_offset = karg32.data_sge_offset;
  2079. karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
  2080. karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
  2081. karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
  2082. karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
  2083. return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
  2084. }
  2085. #endif
  2086. /**
  2087. * _ctl_ioctl_main - main ioctl entry point
  2088. * @file: (struct file)
  2089. * @cmd: ioctl opcode
  2090. * @arg: user space data buffer
  2091. * @compat: handles 32 bit applications in 64bit os
  2092. * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
  2093. * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
  2094. */
  2095. static long
  2096. _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
  2097. u8 compat, u16 mpi_version)
  2098. {
  2099. struct MPT3SAS_ADAPTER *ioc;
  2100. struct mpt3_ioctl_header ioctl_header;
  2101. enum block_state state;
  2102. long ret = -EINVAL;
  2103. /* get IOCTL header */
  2104. if (copy_from_user(&ioctl_header, (char __user *)arg,
  2105. sizeof(struct mpt3_ioctl_header))) {
  2106. pr_err("failure at %s:%d/%s()!\n",
  2107. __FILE__, __LINE__, __func__);
  2108. return -EFAULT;
  2109. }
  2110. if (_ctl_verify_adapter(ioctl_header.ioc_number,
  2111. &ioc, mpi_version) == -1 || !ioc)
  2112. return -ENODEV;
  2113. /* pci_access_mutex lock acquired by ioctl path */
  2114. mutex_lock(&ioc->pci_access_mutex);
  2115. if (ioc->shost_recovery || ioc->pci_error_recovery ||
  2116. ioc->is_driver_loading || ioc->remove_host) {
  2117. ret = -EAGAIN;
  2118. goto out_unlock_pciaccess;
  2119. }
  2120. state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
  2121. if (state == NON_BLOCKING) {
  2122. if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
  2123. ret = -EAGAIN;
  2124. goto out_unlock_pciaccess;
  2125. }
  2126. } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
  2127. ret = -ERESTARTSYS;
  2128. goto out_unlock_pciaccess;
  2129. }
  2130. switch (cmd) {
  2131. case MPT3IOCINFO:
  2132. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
  2133. ret = _ctl_getiocinfo(ioc, arg);
  2134. break;
  2135. #ifdef CONFIG_COMPAT
  2136. case MPT3COMMAND32:
  2137. #endif
  2138. case MPT3COMMAND:
  2139. {
  2140. struct mpt3_ioctl_command __user *uarg;
  2141. struct mpt3_ioctl_command karg;
  2142. #ifdef CONFIG_COMPAT
  2143. if (compat) {
  2144. ret = _ctl_compat_mpt_command(ioc, cmd, arg);
  2145. break;
  2146. }
  2147. #endif
  2148. if (copy_from_user(&karg, arg, sizeof(karg))) {
  2149. pr_err("failure at %s:%d/%s()!\n",
  2150. __FILE__, __LINE__, __func__);
  2151. ret = -EFAULT;
  2152. break;
  2153. }
  2154. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
  2155. uarg = arg;
  2156. ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
  2157. }
  2158. break;
  2159. }
  2160. case MPT3EVENTQUERY:
  2161. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
  2162. ret = _ctl_eventquery(ioc, arg);
  2163. break;
  2164. case MPT3EVENTENABLE:
  2165. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
  2166. ret = _ctl_eventenable(ioc, arg);
  2167. break;
  2168. case MPT3EVENTREPORT:
  2169. ret = _ctl_eventreport(ioc, arg);
  2170. break;
  2171. case MPT3HARDRESET:
  2172. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
  2173. ret = _ctl_do_reset(ioc, arg);
  2174. break;
  2175. case MPT3BTDHMAPPING:
  2176. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
  2177. ret = _ctl_btdh_mapping(ioc, arg);
  2178. break;
  2179. case MPT3DIAGREGISTER:
  2180. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
  2181. ret = _ctl_diag_register(ioc, arg);
  2182. break;
  2183. case MPT3DIAGUNREGISTER:
  2184. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
  2185. ret = _ctl_diag_unregister(ioc, arg);
  2186. break;
  2187. case MPT3DIAGQUERY:
  2188. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
  2189. ret = _ctl_diag_query(ioc, arg);
  2190. break;
  2191. case MPT3DIAGRELEASE:
  2192. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
  2193. ret = _ctl_diag_release(ioc, arg);
  2194. break;
  2195. case MPT3DIAGREADBUFFER:
  2196. if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
  2197. ret = _ctl_diag_read_buffer(ioc, arg);
  2198. break;
  2199. default:
  2200. dctlprintk(ioc, pr_info(MPT3SAS_FMT
  2201. "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
  2202. break;
  2203. }
  2204. mutex_unlock(&ioc->ctl_cmds.mutex);
  2205. out_unlock_pciaccess:
  2206. mutex_unlock(&ioc->pci_access_mutex);
  2207. return ret;
  2208. }
  2209. /**
  2210. * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
  2211. * @file: (struct file)
  2212. * @cmd: ioctl opcode
  2213. * @arg: ?
  2214. */
  2215. static long
  2216. _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2217. {
  2218. long ret;
  2219. /* pass MPI25_VERSION | MPI26_VERSION value,
  2220. * to indicate that this ioctl cmd
  2221. * came from mpt3ctl ioctl device.
  2222. */
  2223. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
  2224. MPI25_VERSION | MPI26_VERSION);
  2225. return ret;
  2226. }
  2227. /**
  2228. * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
  2229. * @file: (struct file)
  2230. * @cmd: ioctl opcode
  2231. * @arg: ?
  2232. */
  2233. static long
  2234. _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2235. {
  2236. long ret;
  2237. /* pass MPI2_VERSION value, to indicate that this ioctl cmd
  2238. * came from mpt2ctl ioctl device.
  2239. */
  2240. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
  2241. return ret;
  2242. }
  2243. #ifdef CONFIG_COMPAT
  2244. /**
  2245. *_ ctl_ioctl_compat - main ioctl entry point (compat)
  2246. * @file: ?
  2247. * @cmd: ?
  2248. * @arg: ?
  2249. *
  2250. * This routine handles 32 bit applications in 64bit os.
  2251. */
  2252. static long
  2253. _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
  2254. {
  2255. long ret;
  2256. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
  2257. MPI25_VERSION | MPI26_VERSION);
  2258. return ret;
  2259. }
  2260. /**
  2261. *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
  2262. * @file: ?
  2263. * @cmd: ?
  2264. * @arg: ?
  2265. *
  2266. * This routine handles 32 bit applications in 64bit os.
  2267. */
  2268. static long
  2269. _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
  2270. {
  2271. long ret;
  2272. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
  2273. return ret;
  2274. }
  2275. #endif
  2276. /* scsi host attributes */
  2277. /**
  2278. * _ctl_version_fw_show - firmware version
  2279. * @cdev: pointer to embedded class device
  2280. * @attr: ?
  2281. * @buf: the buffer returned
  2282. *
  2283. * A sysfs 'read-only' shost attribute.
  2284. */
  2285. static ssize_t
  2286. _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
  2287. char *buf)
  2288. {
  2289. struct Scsi_Host *shost = class_to_shost(cdev);
  2290. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2291. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2292. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  2293. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  2294. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  2295. ioc->facts.FWVersion.Word & 0x000000FF);
  2296. }
  2297. static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
  2298. /**
  2299. * _ctl_version_bios_show - bios version
  2300. * @cdev: pointer to embedded class device
  2301. * @attr: ?
  2302. * @buf: the buffer returned
  2303. *
  2304. * A sysfs 'read-only' shost attribute.
  2305. */
  2306. static ssize_t
  2307. _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
  2308. char *buf)
  2309. {
  2310. struct Scsi_Host *shost = class_to_shost(cdev);
  2311. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2312. u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  2313. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2314. (version & 0xFF000000) >> 24,
  2315. (version & 0x00FF0000) >> 16,
  2316. (version & 0x0000FF00) >> 8,
  2317. version & 0x000000FF);
  2318. }
  2319. static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
  2320. /**
  2321. * _ctl_version_mpi_show - MPI (message passing interface) version
  2322. * @cdev: pointer to embedded class device
  2323. * @attr: ?
  2324. * @buf: the buffer returned
  2325. *
  2326. * A sysfs 'read-only' shost attribute.
  2327. */
  2328. static ssize_t
  2329. _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
  2330. char *buf)
  2331. {
  2332. struct Scsi_Host *shost = class_to_shost(cdev);
  2333. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2334. return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
  2335. ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
  2336. }
  2337. static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
  2338. /**
  2339. * _ctl_version_product_show - product name
  2340. * @cdev: pointer to embedded class device
  2341. * @attr: ?
  2342. * @buf: the buffer returned
  2343. *
  2344. * A sysfs 'read-only' shost attribute.
  2345. */
  2346. static ssize_t
  2347. _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
  2348. char *buf)
  2349. {
  2350. struct Scsi_Host *shost = class_to_shost(cdev);
  2351. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2352. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
  2353. }
  2354. static DEVICE_ATTR(version_product, S_IRUGO, _ctl_version_product_show, NULL);
  2355. /**
  2356. * _ctl_version_nvdata_persistent_show - ndvata persistent version
  2357. * @cdev: pointer to embedded class device
  2358. * @attr: ?
  2359. * @buf: the buffer returned
  2360. *
  2361. * A sysfs 'read-only' shost attribute.
  2362. */
  2363. static ssize_t
  2364. _ctl_version_nvdata_persistent_show(struct device *cdev,
  2365. struct device_attribute *attr, char *buf)
  2366. {
  2367. struct Scsi_Host *shost = class_to_shost(cdev);
  2368. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2369. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2370. le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
  2371. }
  2372. static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
  2373. _ctl_version_nvdata_persistent_show, NULL);
  2374. /**
  2375. * _ctl_version_nvdata_default_show - nvdata default version
  2376. * @cdev: pointer to embedded class device
  2377. * @attr: ?
  2378. * @buf: the buffer returned
  2379. *
  2380. * A sysfs 'read-only' shost attribute.
  2381. */
  2382. static ssize_t
  2383. _ctl_version_nvdata_default_show(struct device *cdev, struct device_attribute
  2384. *attr, char *buf)
  2385. {
  2386. struct Scsi_Host *shost = class_to_shost(cdev);
  2387. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2388. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2389. le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
  2390. }
  2391. static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
  2392. _ctl_version_nvdata_default_show, NULL);
  2393. /**
  2394. * _ctl_board_name_show - board name
  2395. * @cdev: pointer to embedded class device
  2396. * @attr: ?
  2397. * @buf: the buffer returned
  2398. *
  2399. * A sysfs 'read-only' shost attribute.
  2400. */
  2401. static ssize_t
  2402. _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
  2403. char *buf)
  2404. {
  2405. struct Scsi_Host *shost = class_to_shost(cdev);
  2406. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2407. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
  2408. }
  2409. static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
  2410. /**
  2411. * _ctl_board_assembly_show - board assembly name
  2412. * @cdev: pointer to embedded class device
  2413. * @attr: ?
  2414. * @buf: the buffer returned
  2415. *
  2416. * A sysfs 'read-only' shost attribute.
  2417. */
  2418. static ssize_t
  2419. _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
  2420. char *buf)
  2421. {
  2422. struct Scsi_Host *shost = class_to_shost(cdev);
  2423. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2424. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
  2425. }
  2426. static DEVICE_ATTR(board_assembly, S_IRUGO, _ctl_board_assembly_show, NULL);
  2427. /**
  2428. * _ctl_board_tracer_show - board tracer number
  2429. * @cdev: pointer to embedded class device
  2430. * @attr: ?
  2431. * @buf: the buffer returned
  2432. *
  2433. * A sysfs 'read-only' shost attribute.
  2434. */
  2435. static ssize_t
  2436. _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
  2437. char *buf)
  2438. {
  2439. struct Scsi_Host *shost = class_to_shost(cdev);
  2440. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2441. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
  2442. }
  2443. static DEVICE_ATTR(board_tracer, S_IRUGO, _ctl_board_tracer_show, NULL);
  2444. /**
  2445. * _ctl_io_delay_show - io missing delay
  2446. * @cdev: pointer to embedded class device
  2447. * @attr: ?
  2448. * @buf: the buffer returned
  2449. *
  2450. * This is for firmware implemention for deboucing device
  2451. * removal events.
  2452. *
  2453. * A sysfs 'read-only' shost attribute.
  2454. */
  2455. static ssize_t
  2456. _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
  2457. char *buf)
  2458. {
  2459. struct Scsi_Host *shost = class_to_shost(cdev);
  2460. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2461. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
  2462. }
  2463. static DEVICE_ATTR(io_delay, S_IRUGO, _ctl_io_delay_show, NULL);
  2464. /**
  2465. * _ctl_device_delay_show - device missing delay
  2466. * @cdev: pointer to embedded class device
  2467. * @attr: ?
  2468. * @buf: the buffer returned
  2469. *
  2470. * This is for firmware implemention for deboucing device
  2471. * removal events.
  2472. *
  2473. * A sysfs 'read-only' shost attribute.
  2474. */
  2475. static ssize_t
  2476. _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
  2477. char *buf)
  2478. {
  2479. struct Scsi_Host *shost = class_to_shost(cdev);
  2480. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2481. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
  2482. }
  2483. static DEVICE_ATTR(device_delay, S_IRUGO, _ctl_device_delay_show, NULL);
  2484. /**
  2485. * _ctl_fw_queue_depth_show - global credits
  2486. * @cdev: pointer to embedded class device
  2487. * @attr: ?
  2488. * @buf: the buffer returned
  2489. *
  2490. * This is firmware queue depth limit
  2491. *
  2492. * A sysfs 'read-only' shost attribute.
  2493. */
  2494. static ssize_t
  2495. _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
  2496. char *buf)
  2497. {
  2498. struct Scsi_Host *shost = class_to_shost(cdev);
  2499. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2500. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
  2501. }
  2502. static DEVICE_ATTR(fw_queue_depth, S_IRUGO, _ctl_fw_queue_depth_show, NULL);
  2503. /**
  2504. * _ctl_sas_address_show - sas address
  2505. * @cdev: pointer to embedded class device
  2506. * @attr: ?
  2507. * @buf: the buffer returned
  2508. *
  2509. * This is the controller sas address
  2510. *
  2511. * A sysfs 'read-only' shost attribute.
  2512. */
  2513. static ssize_t
  2514. _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
  2515. char *buf)
  2516. {
  2517. struct Scsi_Host *shost = class_to_shost(cdev);
  2518. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2519. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  2520. (unsigned long long)ioc->sas_hba.sas_address);
  2521. }
  2522. static DEVICE_ATTR(host_sas_address, S_IRUGO,
  2523. _ctl_host_sas_address_show, NULL);
  2524. /**
  2525. * _ctl_logging_level_show - logging level
  2526. * @cdev: pointer to embedded class device
  2527. * @attr: ?
  2528. * @buf: the buffer returned
  2529. *
  2530. * A sysfs 'read/write' shost attribute.
  2531. */
  2532. static ssize_t
  2533. _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
  2534. char *buf)
  2535. {
  2536. struct Scsi_Host *shost = class_to_shost(cdev);
  2537. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2538. return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
  2539. }
  2540. static ssize_t
  2541. _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
  2542. const char *buf, size_t count)
  2543. {
  2544. struct Scsi_Host *shost = class_to_shost(cdev);
  2545. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2546. int val = 0;
  2547. if (sscanf(buf, "%x", &val) != 1)
  2548. return -EINVAL;
  2549. ioc->logging_level = val;
  2550. pr_info(MPT3SAS_FMT "logging_level=%08xh\n", ioc->name,
  2551. ioc->logging_level);
  2552. return strlen(buf);
  2553. }
  2554. static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR, _ctl_logging_level_show,
  2555. _ctl_logging_level_store);
  2556. /**
  2557. * _ctl_fwfault_debug_show - show/store fwfault_debug
  2558. * @cdev: pointer to embedded class device
  2559. * @attr: ?
  2560. * @buf: the buffer returned
  2561. *
  2562. * mpt3sas_fwfault_debug is command line option
  2563. * A sysfs 'read/write' shost attribute.
  2564. */
  2565. static ssize_t
  2566. _ctl_fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
  2567. char *buf)
  2568. {
  2569. struct Scsi_Host *shost = class_to_shost(cdev);
  2570. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2571. return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
  2572. }
  2573. static ssize_t
  2574. _ctl_fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
  2575. const char *buf, size_t count)
  2576. {
  2577. struct Scsi_Host *shost = class_to_shost(cdev);
  2578. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2579. int val = 0;
  2580. if (sscanf(buf, "%d", &val) != 1)
  2581. return -EINVAL;
  2582. ioc->fwfault_debug = val;
  2583. pr_info(MPT3SAS_FMT "fwfault_debug=%d\n", ioc->name,
  2584. ioc->fwfault_debug);
  2585. return strlen(buf);
  2586. }
  2587. static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
  2588. _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
  2589. /**
  2590. * _ctl_ioc_reset_count_show - ioc reset count
  2591. * @cdev: pointer to embedded class device
  2592. * @attr: ?
  2593. * @buf: the buffer returned
  2594. *
  2595. * This is firmware queue depth limit
  2596. *
  2597. * A sysfs 'read-only' shost attribute.
  2598. */
  2599. static ssize_t
  2600. _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
  2601. char *buf)
  2602. {
  2603. struct Scsi_Host *shost = class_to_shost(cdev);
  2604. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2605. return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
  2606. }
  2607. static DEVICE_ATTR(ioc_reset_count, S_IRUGO, _ctl_ioc_reset_count_show, NULL);
  2608. /**
  2609. * _ctl_ioc_reply_queue_count_show - number of reply queues
  2610. * @cdev: pointer to embedded class device
  2611. * @attr: ?
  2612. * @buf: the buffer returned
  2613. *
  2614. * This is number of reply queues
  2615. *
  2616. * A sysfs 'read-only' shost attribute.
  2617. */
  2618. static ssize_t
  2619. _ctl_ioc_reply_queue_count_show(struct device *cdev,
  2620. struct device_attribute *attr, char *buf)
  2621. {
  2622. u8 reply_queue_count;
  2623. struct Scsi_Host *shost = class_to_shost(cdev);
  2624. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2625. if ((ioc->facts.IOCCapabilities &
  2626. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
  2627. reply_queue_count = ioc->reply_queue_count;
  2628. else
  2629. reply_queue_count = 1;
  2630. return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
  2631. }
  2632. static DEVICE_ATTR(reply_queue_count, S_IRUGO, _ctl_ioc_reply_queue_count_show,
  2633. NULL);
  2634. /**
  2635. * _ctl_BRM_status_show - Backup Rail Monitor Status
  2636. * @cdev: pointer to embedded class device
  2637. * @attr: ?
  2638. * @buf: the buffer returned
  2639. *
  2640. * This is number of reply queues
  2641. *
  2642. * A sysfs 'read-only' shost attribute.
  2643. */
  2644. static ssize_t
  2645. _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
  2646. char *buf)
  2647. {
  2648. struct Scsi_Host *shost = class_to_shost(cdev);
  2649. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2650. Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
  2651. Mpi2ConfigReply_t mpi_reply;
  2652. u16 backup_rail_monitor_status = 0;
  2653. u16 ioc_status;
  2654. int sz;
  2655. ssize_t rc = 0;
  2656. if (!ioc->is_warpdrive) {
  2657. pr_err(MPT3SAS_FMT "%s: BRM attribute is only for"
  2658. " warpdrive\n", ioc->name, __func__);
  2659. goto out;
  2660. }
  2661. /* pci_access_mutex lock acquired by sysfs show path */
  2662. mutex_lock(&ioc->pci_access_mutex);
  2663. if (ioc->pci_error_recovery || ioc->remove_host) {
  2664. mutex_unlock(&ioc->pci_access_mutex);
  2665. return 0;
  2666. }
  2667. /* allocate upto GPIOVal 36 entries */
  2668. sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
  2669. io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
  2670. if (!io_unit_pg3) {
  2671. pr_err(MPT3SAS_FMT "%s: failed allocating memory "
  2672. "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
  2673. goto out;
  2674. }
  2675. if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
  2676. 0) {
  2677. pr_err(MPT3SAS_FMT
  2678. "%s: failed reading iounit_pg3\n", ioc->name,
  2679. __func__);
  2680. goto out;
  2681. }
  2682. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  2683. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2684. pr_err(MPT3SAS_FMT "%s: iounit_pg3 failed with "
  2685. "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
  2686. goto out;
  2687. }
  2688. if (io_unit_pg3->GPIOCount < 25) {
  2689. pr_err(MPT3SAS_FMT "%s: iounit_pg3->GPIOCount less than "
  2690. "25 entries, detected (%d) entries\n", ioc->name, __func__,
  2691. io_unit_pg3->GPIOCount);
  2692. goto out;
  2693. }
  2694. /* BRM status is in bit zero of GPIOVal[24] */
  2695. backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
  2696. rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
  2697. out:
  2698. kfree(io_unit_pg3);
  2699. mutex_unlock(&ioc->pci_access_mutex);
  2700. return rc;
  2701. }
  2702. static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
  2703. struct DIAG_BUFFER_START {
  2704. __le32 Size;
  2705. __le32 DiagVersion;
  2706. u8 BufferType;
  2707. u8 Reserved[3];
  2708. __le32 Reserved1;
  2709. __le32 Reserved2;
  2710. __le32 Reserved3;
  2711. };
  2712. /**
  2713. * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
  2714. * @cdev: pointer to embedded class device
  2715. * @attr: ?
  2716. * @buf: the buffer returned
  2717. *
  2718. * A sysfs 'read-only' shost attribute.
  2719. */
  2720. static ssize_t
  2721. _ctl_host_trace_buffer_size_show(struct device *cdev,
  2722. struct device_attribute *attr, char *buf)
  2723. {
  2724. struct Scsi_Host *shost = class_to_shost(cdev);
  2725. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2726. u32 size = 0;
  2727. struct DIAG_BUFFER_START *request_data;
  2728. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2729. pr_err(MPT3SAS_FMT
  2730. "%s: host_trace_buffer is not registered\n",
  2731. ioc->name, __func__);
  2732. return 0;
  2733. }
  2734. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2735. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2736. pr_err(MPT3SAS_FMT
  2737. "%s: host_trace_buffer is not registered\n",
  2738. ioc->name, __func__);
  2739. return 0;
  2740. }
  2741. request_data = (struct DIAG_BUFFER_START *)
  2742. ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
  2743. if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
  2744. le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
  2745. le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
  2746. le32_to_cpu(request_data->Reserved3) == 0x4742444c)
  2747. size = le32_to_cpu(request_data->Size);
  2748. ioc->ring_buffer_sz = size;
  2749. return snprintf(buf, PAGE_SIZE, "%d\n", size);
  2750. }
  2751. static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
  2752. _ctl_host_trace_buffer_size_show, NULL);
  2753. /**
  2754. * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
  2755. * @cdev: pointer to embedded class device
  2756. * @attr: ?
  2757. * @buf: the buffer returned
  2758. *
  2759. * A sysfs 'read/write' shost attribute.
  2760. *
  2761. * You will only be able to read 4k bytes of ring buffer at a time.
  2762. * In order to read beyond 4k bytes, you will have to write out the
  2763. * offset to the same attribute, it will move the pointer.
  2764. */
  2765. static ssize_t
  2766. _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
  2767. char *buf)
  2768. {
  2769. struct Scsi_Host *shost = class_to_shost(cdev);
  2770. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2771. void *request_data;
  2772. u32 size;
  2773. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2774. pr_err(MPT3SAS_FMT
  2775. "%s: host_trace_buffer is not registered\n",
  2776. ioc->name, __func__);
  2777. return 0;
  2778. }
  2779. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2780. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2781. pr_err(MPT3SAS_FMT
  2782. "%s: host_trace_buffer is not registered\n",
  2783. ioc->name, __func__);
  2784. return 0;
  2785. }
  2786. if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
  2787. return 0;
  2788. size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
  2789. size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
  2790. request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
  2791. memcpy(buf, request_data, size);
  2792. return size;
  2793. }
  2794. static ssize_t
  2795. _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
  2796. const char *buf, size_t count)
  2797. {
  2798. struct Scsi_Host *shost = class_to_shost(cdev);
  2799. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2800. int val = 0;
  2801. if (sscanf(buf, "%d", &val) != 1)
  2802. return -EINVAL;
  2803. ioc->ring_buffer_offset = val;
  2804. return strlen(buf);
  2805. }
  2806. static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
  2807. _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
  2808. /*****************************************/
  2809. /**
  2810. * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
  2811. * @cdev: pointer to embedded class device
  2812. * @attr: ?
  2813. * @buf: the buffer returned
  2814. *
  2815. * A sysfs 'read/write' shost attribute.
  2816. *
  2817. * This is a mechnism to post/release host_trace_buffers
  2818. */
  2819. static ssize_t
  2820. _ctl_host_trace_buffer_enable_show(struct device *cdev,
  2821. struct device_attribute *attr, char *buf)
  2822. {
  2823. struct Scsi_Host *shost = class_to_shost(cdev);
  2824. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2825. if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
  2826. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2827. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
  2828. return snprintf(buf, PAGE_SIZE, "off\n");
  2829. else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2830. MPT3_DIAG_BUFFER_IS_RELEASED))
  2831. return snprintf(buf, PAGE_SIZE, "release\n");
  2832. else
  2833. return snprintf(buf, PAGE_SIZE, "post\n");
  2834. }
  2835. static ssize_t
  2836. _ctl_host_trace_buffer_enable_store(struct device *cdev,
  2837. struct device_attribute *attr, const char *buf, size_t count)
  2838. {
  2839. struct Scsi_Host *shost = class_to_shost(cdev);
  2840. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2841. char str[10] = "";
  2842. struct mpt3_diag_register diag_register;
  2843. u8 issue_reset = 0;
  2844. /* don't allow post/release occurr while recovery is active */
  2845. if (ioc->shost_recovery || ioc->remove_host ||
  2846. ioc->pci_error_recovery || ioc->is_driver_loading)
  2847. return -EBUSY;
  2848. if (sscanf(buf, "%9s", str) != 1)
  2849. return -EINVAL;
  2850. if (!strcmp(str, "post")) {
  2851. /* exit out if host buffers are already posted */
  2852. if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
  2853. (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2854. MPT3_DIAG_BUFFER_IS_REGISTERED) &&
  2855. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2856. MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
  2857. goto out;
  2858. memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
  2859. pr_info(MPT3SAS_FMT "posting host trace buffers\n",
  2860. ioc->name);
  2861. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  2862. diag_register.requested_buffer_size = (1024 * 1024);
  2863. diag_register.unique_id = 0x7075900;
  2864. ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
  2865. _ctl_diag_register_2(ioc, &diag_register);
  2866. } else if (!strcmp(str, "release")) {
  2867. /* exit out if host buffers are already released */
  2868. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
  2869. goto out;
  2870. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2871. MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
  2872. goto out;
  2873. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2874. MPT3_DIAG_BUFFER_IS_RELEASED))
  2875. goto out;
  2876. pr_info(MPT3SAS_FMT "releasing host trace buffer\n",
  2877. ioc->name);
  2878. mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
  2879. &issue_reset);
  2880. }
  2881. out:
  2882. return strlen(buf);
  2883. }
  2884. static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
  2885. _ctl_host_trace_buffer_enable_show,
  2886. _ctl_host_trace_buffer_enable_store);
  2887. /*********** diagnostic trigger suppport *********************************/
  2888. /**
  2889. * _ctl_diag_trigger_master_show - show the diag_trigger_master attribute
  2890. * @cdev: pointer to embedded class device
  2891. * @attr: ?
  2892. * @buf: the buffer returned
  2893. *
  2894. * A sysfs 'read/write' shost attribute.
  2895. */
  2896. static ssize_t
  2897. _ctl_diag_trigger_master_show(struct device *cdev,
  2898. struct device_attribute *attr, char *buf)
  2899. {
  2900. struct Scsi_Host *shost = class_to_shost(cdev);
  2901. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2902. unsigned long flags;
  2903. ssize_t rc;
  2904. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2905. rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
  2906. memcpy(buf, &ioc->diag_trigger_master, rc);
  2907. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2908. return rc;
  2909. }
  2910. /**
  2911. * _ctl_diag_trigger_master_store - store the diag_trigger_master attribute
  2912. * @cdev: pointer to embedded class device
  2913. * @attr: ?
  2914. * @buf: the buffer returned
  2915. * @count: ?
  2916. *
  2917. * A sysfs 'read/write' shost attribute.
  2918. */
  2919. static ssize_t
  2920. _ctl_diag_trigger_master_store(struct device *cdev,
  2921. struct device_attribute *attr, const char *buf, size_t count)
  2922. {
  2923. struct Scsi_Host *shost = class_to_shost(cdev);
  2924. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2925. unsigned long flags;
  2926. ssize_t rc;
  2927. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2928. rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
  2929. memset(&ioc->diag_trigger_master, 0,
  2930. sizeof(struct SL_WH_MASTER_TRIGGER_T));
  2931. memcpy(&ioc->diag_trigger_master, buf, rc);
  2932. ioc->diag_trigger_master.MasterData |=
  2933. (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
  2934. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2935. return rc;
  2936. }
  2937. static DEVICE_ATTR(diag_trigger_master, S_IRUGO | S_IWUSR,
  2938. _ctl_diag_trigger_master_show, _ctl_diag_trigger_master_store);
  2939. /**
  2940. * _ctl_diag_trigger_event_show - show the diag_trigger_event attribute
  2941. * @cdev: pointer to embedded class device
  2942. * @attr: ?
  2943. * @buf: the buffer returned
  2944. *
  2945. * A sysfs 'read/write' shost attribute.
  2946. */
  2947. static ssize_t
  2948. _ctl_diag_trigger_event_show(struct device *cdev,
  2949. struct device_attribute *attr, char *buf)
  2950. {
  2951. struct Scsi_Host *shost = class_to_shost(cdev);
  2952. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2953. unsigned long flags;
  2954. ssize_t rc;
  2955. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2956. rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
  2957. memcpy(buf, &ioc->diag_trigger_event, rc);
  2958. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2959. return rc;
  2960. }
  2961. /**
  2962. * _ctl_diag_trigger_event_store - store the diag_trigger_event attribute
  2963. * @cdev: pointer to embedded class device
  2964. * @attr: ?
  2965. * @buf: the buffer returned
  2966. * @count: ?
  2967. *
  2968. * A sysfs 'read/write' shost attribute.
  2969. */
  2970. static ssize_t
  2971. _ctl_diag_trigger_event_store(struct device *cdev,
  2972. struct device_attribute *attr, const char *buf, size_t count)
  2973. {
  2974. struct Scsi_Host *shost = class_to_shost(cdev);
  2975. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2976. unsigned long flags;
  2977. ssize_t sz;
  2978. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  2979. sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
  2980. memset(&ioc->diag_trigger_event, 0,
  2981. sizeof(struct SL_WH_EVENT_TRIGGERS_T));
  2982. memcpy(&ioc->diag_trigger_event, buf, sz);
  2983. if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
  2984. ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
  2985. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  2986. return sz;
  2987. }
  2988. static DEVICE_ATTR(diag_trigger_event, S_IRUGO | S_IWUSR,
  2989. _ctl_diag_trigger_event_show, _ctl_diag_trigger_event_store);
  2990. /**
  2991. * _ctl_diag_trigger_scsi_show - show the diag_trigger_scsi attribute
  2992. * @cdev: pointer to embedded class device
  2993. * @attr: ?
  2994. * @buf: the buffer returned
  2995. *
  2996. * A sysfs 'read/write' shost attribute.
  2997. */
  2998. static ssize_t
  2999. _ctl_diag_trigger_scsi_show(struct device *cdev,
  3000. struct device_attribute *attr, char *buf)
  3001. {
  3002. struct Scsi_Host *shost = class_to_shost(cdev);
  3003. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  3004. unsigned long flags;
  3005. ssize_t rc;
  3006. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  3007. rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
  3008. memcpy(buf, &ioc->diag_trigger_scsi, rc);
  3009. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  3010. return rc;
  3011. }
  3012. /**
  3013. * _ctl_diag_trigger_scsi_store - store the diag_trigger_scsi attribute
  3014. * @cdev: pointer to embedded class device
  3015. * @attr: ?
  3016. * @buf: the buffer returned
  3017. * @count: ?
  3018. *
  3019. * A sysfs 'read/write' shost attribute.
  3020. */
  3021. static ssize_t
  3022. _ctl_diag_trigger_scsi_store(struct device *cdev,
  3023. struct device_attribute *attr, const char *buf, size_t count)
  3024. {
  3025. struct Scsi_Host *shost = class_to_shost(cdev);
  3026. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  3027. unsigned long flags;
  3028. ssize_t sz;
  3029. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  3030. sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
  3031. memset(&ioc->diag_trigger_scsi, 0,
  3032. sizeof(struct SL_WH_EVENT_TRIGGERS_T));
  3033. memcpy(&ioc->diag_trigger_scsi, buf, sz);
  3034. if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
  3035. ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
  3036. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  3037. return sz;
  3038. }
  3039. static DEVICE_ATTR(diag_trigger_scsi, S_IRUGO | S_IWUSR,
  3040. _ctl_diag_trigger_scsi_show, _ctl_diag_trigger_scsi_store);
  3041. /**
  3042. * _ctl_diag_trigger_scsi_show - show the diag_trigger_mpi attribute
  3043. * @cdev: pointer to embedded class device
  3044. * @attr: ?
  3045. * @buf: the buffer returned
  3046. *
  3047. * A sysfs 'read/write' shost attribute.
  3048. */
  3049. static ssize_t
  3050. _ctl_diag_trigger_mpi_show(struct device *cdev,
  3051. struct device_attribute *attr, char *buf)
  3052. {
  3053. struct Scsi_Host *shost = class_to_shost(cdev);
  3054. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  3055. unsigned long flags;
  3056. ssize_t rc;
  3057. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  3058. rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
  3059. memcpy(buf, &ioc->diag_trigger_mpi, rc);
  3060. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  3061. return rc;
  3062. }
  3063. /**
  3064. * _ctl_diag_trigger_mpi_store - store the diag_trigger_mpi attribute
  3065. * @cdev: pointer to embedded class device
  3066. * @attr: ?
  3067. * @buf: the buffer returned
  3068. * @count: ?
  3069. *
  3070. * A sysfs 'read/write' shost attribute.
  3071. */
  3072. static ssize_t
  3073. _ctl_diag_trigger_mpi_store(struct device *cdev,
  3074. struct device_attribute *attr, const char *buf, size_t count)
  3075. {
  3076. struct Scsi_Host *shost = class_to_shost(cdev);
  3077. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  3078. unsigned long flags;
  3079. ssize_t sz;
  3080. spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
  3081. sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
  3082. memset(&ioc->diag_trigger_mpi, 0,
  3083. sizeof(ioc->diag_trigger_mpi));
  3084. memcpy(&ioc->diag_trigger_mpi, buf, sz);
  3085. if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
  3086. ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
  3087. spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
  3088. return sz;
  3089. }
  3090. static DEVICE_ATTR(diag_trigger_mpi, S_IRUGO | S_IWUSR,
  3091. _ctl_diag_trigger_mpi_show, _ctl_diag_trigger_mpi_store);
  3092. /*********** diagnostic trigger suppport *** END ****************************/
  3093. /*****************************************/
  3094. struct device_attribute *mpt3sas_host_attrs[] = {
  3095. &dev_attr_version_fw,
  3096. &dev_attr_version_bios,
  3097. &dev_attr_version_mpi,
  3098. &dev_attr_version_product,
  3099. &dev_attr_version_nvdata_persistent,
  3100. &dev_attr_version_nvdata_default,
  3101. &dev_attr_board_name,
  3102. &dev_attr_board_assembly,
  3103. &dev_attr_board_tracer,
  3104. &dev_attr_io_delay,
  3105. &dev_attr_device_delay,
  3106. &dev_attr_logging_level,
  3107. &dev_attr_fwfault_debug,
  3108. &dev_attr_fw_queue_depth,
  3109. &dev_attr_host_sas_address,
  3110. &dev_attr_ioc_reset_count,
  3111. &dev_attr_host_trace_buffer_size,
  3112. &dev_attr_host_trace_buffer,
  3113. &dev_attr_host_trace_buffer_enable,
  3114. &dev_attr_reply_queue_count,
  3115. &dev_attr_diag_trigger_master,
  3116. &dev_attr_diag_trigger_event,
  3117. &dev_attr_diag_trigger_scsi,
  3118. &dev_attr_diag_trigger_mpi,
  3119. &dev_attr_BRM_status,
  3120. NULL,
  3121. };
  3122. /* device attributes */
  3123. /**
  3124. * _ctl_device_sas_address_show - sas address
  3125. * @dev: pointer to embedded class device
  3126. * @attr: ?
  3127. * @buf: the buffer returned
  3128. *
  3129. * This is the sas address for the target
  3130. *
  3131. * A sysfs 'read-only' shost attribute.
  3132. */
  3133. static ssize_t
  3134. _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
  3135. char *buf)
  3136. {
  3137. struct scsi_device *sdev = to_scsi_device(dev);
  3138. struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  3139. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  3140. (unsigned long long)sas_device_priv_data->sas_target->sas_address);
  3141. }
  3142. static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
  3143. /**
  3144. * _ctl_device_handle_show - device handle
  3145. * @dev: pointer to embedded class device
  3146. * @attr: ?
  3147. * @buf: the buffer returned
  3148. *
  3149. * This is the firmware assigned device handle
  3150. *
  3151. * A sysfs 'read-only' shost attribute.
  3152. */
  3153. static ssize_t
  3154. _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
  3155. char *buf)
  3156. {
  3157. struct scsi_device *sdev = to_scsi_device(dev);
  3158. struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  3159. return snprintf(buf, PAGE_SIZE, "0x%04x\n",
  3160. sas_device_priv_data->sas_target->handle);
  3161. }
  3162. static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
  3163. /**
  3164. * _ctl_device_ncq_io_prio_show - send prioritized io commands to device
  3165. * @dev: pointer to embedded device
  3166. * @attr: ?
  3167. * @buf: the buffer returned
  3168. *
  3169. * A sysfs 'read/write' sdev attribute, only works with SATA
  3170. */
  3171. static ssize_t
  3172. _ctl_device_ncq_prio_enable_show(struct device *dev,
  3173. struct device_attribute *attr, char *buf)
  3174. {
  3175. struct scsi_device *sdev = to_scsi_device(dev);
  3176. struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  3177. return snprintf(buf, PAGE_SIZE, "%d\n",
  3178. sas_device_priv_data->ncq_prio_enable);
  3179. }
  3180. static ssize_t
  3181. _ctl_device_ncq_prio_enable_store(struct device *dev,
  3182. struct device_attribute *attr,
  3183. const char *buf, size_t count)
  3184. {
  3185. struct scsi_device *sdev = to_scsi_device(dev);
  3186. struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  3187. bool ncq_prio_enable = 0;
  3188. if (kstrtobool(buf, &ncq_prio_enable))
  3189. return -EINVAL;
  3190. if (!scsih_ncq_prio_supp(sdev))
  3191. return -EINVAL;
  3192. sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
  3193. return strlen(buf);
  3194. }
  3195. static DEVICE_ATTR(sas_ncq_prio_enable, S_IRUGO | S_IWUSR,
  3196. _ctl_device_ncq_prio_enable_show,
  3197. _ctl_device_ncq_prio_enable_store);
  3198. struct device_attribute *mpt3sas_dev_attrs[] = {
  3199. &dev_attr_sas_address,
  3200. &dev_attr_sas_device_handle,
  3201. &dev_attr_sas_ncq_prio_enable,
  3202. NULL,
  3203. };
  3204. /* file operations table for mpt3ctl device */
  3205. static const struct file_operations ctl_fops = {
  3206. .owner = THIS_MODULE,
  3207. .unlocked_ioctl = _ctl_ioctl,
  3208. .poll = _ctl_poll,
  3209. .fasync = _ctl_fasync,
  3210. #ifdef CONFIG_COMPAT
  3211. .compat_ioctl = _ctl_ioctl_compat,
  3212. #endif
  3213. };
  3214. /* file operations table for mpt2ctl device */
  3215. static const struct file_operations ctl_gen2_fops = {
  3216. .owner = THIS_MODULE,
  3217. .unlocked_ioctl = _ctl_mpt2_ioctl,
  3218. .poll = _ctl_poll,
  3219. .fasync = _ctl_fasync,
  3220. #ifdef CONFIG_COMPAT
  3221. .compat_ioctl = _ctl_mpt2_ioctl_compat,
  3222. #endif
  3223. };
  3224. static struct miscdevice ctl_dev = {
  3225. .minor = MPT3SAS_MINOR,
  3226. .name = MPT3SAS_DEV_NAME,
  3227. .fops = &ctl_fops,
  3228. };
  3229. static struct miscdevice gen2_ctl_dev = {
  3230. .minor = MPT2SAS_MINOR,
  3231. .name = MPT2SAS_DEV_NAME,
  3232. .fops = &ctl_gen2_fops,
  3233. };
  3234. /**
  3235. * mpt3sas_ctl_init - main entry point for ctl.
  3236. * @hbas_to_enumerate: ?
  3237. */
  3238. void
  3239. mpt3sas_ctl_init(ushort hbas_to_enumerate)
  3240. {
  3241. async_queue = NULL;
  3242. /* Don't register mpt3ctl ioctl device if
  3243. * hbas_to_enumarate is one.
  3244. */
  3245. if (hbas_to_enumerate != 1)
  3246. if (misc_register(&ctl_dev) < 0)
  3247. pr_err("%s can't register misc device [minor=%d]\n",
  3248. MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
  3249. /* Don't register mpt3ctl ioctl device if
  3250. * hbas_to_enumarate is two.
  3251. */
  3252. if (hbas_to_enumerate != 2)
  3253. if (misc_register(&gen2_ctl_dev) < 0)
  3254. pr_err("%s can't register misc device [minor=%d]\n",
  3255. MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
  3256. init_waitqueue_head(&ctl_poll_wait);
  3257. }
  3258. /**
  3259. * mpt3sas_ctl_exit - exit point for ctl
  3260. * @hbas_to_enumerate: ?
  3261. */
  3262. void
  3263. mpt3sas_ctl_exit(ushort hbas_to_enumerate)
  3264. {
  3265. struct MPT3SAS_ADAPTER *ioc;
  3266. int i;
  3267. list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
  3268. /* free memory associated to diag buffers */
  3269. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  3270. if (!ioc->diag_buffer[i])
  3271. continue;
  3272. if (!(ioc->diag_buffer_status[i] &
  3273. MPT3_DIAG_BUFFER_IS_REGISTERED))
  3274. continue;
  3275. if ((ioc->diag_buffer_status[i] &
  3276. MPT3_DIAG_BUFFER_IS_RELEASED))
  3277. continue;
  3278. pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
  3279. ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
  3280. ioc->diag_buffer[i] = NULL;
  3281. ioc->diag_buffer_status[i] = 0;
  3282. }
  3283. kfree(ioc->event_log);
  3284. }
  3285. if (hbas_to_enumerate != 1)
  3286. misc_deregister(&ctl_dev);
  3287. if (hbas_to_enumerate != 2)
  3288. misc_deregister(&gen2_ctl_dev);
  3289. }