mpt2sas_ctl.c 87 KB

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