mpt2sas_ctl.c 87 KB

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