mpt3sas_ctl.c 96 KB

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