mpt3sas_ctl.c 91 KB

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