mpt3sas_ctl.c 101 KB

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