mpt2sas_ctl.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059
  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-2010 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. * @state - NON_BLOCKING or BLOCKING
  570. */
  571. static long
  572. _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc,
  573. struct mpt2_ioctl_command karg, void __user *mf, enum block_state state)
  574. {
  575. MPI2RequestHeader_t *mpi_request = NULL, *request;
  576. MPI2DefaultReply_t *mpi_reply;
  577. u32 ioc_state;
  578. u16 ioc_status;
  579. u16 smid;
  580. unsigned long timeout, timeleft;
  581. u8 issue_reset;
  582. u32 sz;
  583. void *psge;
  584. void *data_out = NULL;
  585. dma_addr_t data_out_dma;
  586. size_t data_out_sz = 0;
  587. void *data_in = NULL;
  588. dma_addr_t data_in_dma;
  589. size_t data_in_sz = 0;
  590. u32 sgl_flags;
  591. long ret;
  592. u16 wait_state_count;
  593. issue_reset = 0;
  594. if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
  595. return -EAGAIN;
  596. else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
  597. return -ERESTARTSYS;
  598. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  599. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  600. ioc->name, __func__);
  601. ret = -EAGAIN;
  602. goto out;
  603. }
  604. wait_state_count = 0;
  605. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  606. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  607. if (wait_state_count++ == 10) {
  608. printk(MPT2SAS_ERR_FMT
  609. "%s: failed due to ioc not operational\n",
  610. ioc->name, __func__);
  611. ret = -EFAULT;
  612. goto out;
  613. }
  614. ssleep(1);
  615. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  616. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  617. "operational state(count=%d)\n", ioc->name,
  618. __func__, wait_state_count);
  619. }
  620. if (wait_state_count)
  621. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  622. ioc->name, __func__);
  623. mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
  624. if (!mpi_request) {
  625. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
  626. "mpi_request\n", ioc->name, __func__);
  627. ret = -ENOMEM;
  628. goto out;
  629. }
  630. /* Check for overflow and wraparound */
  631. if (karg.data_sge_offset * 4 > ioc->request_sz ||
  632. karg.data_sge_offset > (UINT_MAX / 4)) {
  633. ret = -EINVAL;
  634. goto out;
  635. }
  636. /* copy in request message frame from user */
  637. if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
  638. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
  639. __func__);
  640. ret = -EFAULT;
  641. goto out;
  642. }
  643. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  644. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
  645. if (!smid) {
  646. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  647. ioc->name, __func__);
  648. ret = -EAGAIN;
  649. goto out;
  650. }
  651. } else {
  652. smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
  653. if (!smid) {
  654. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  655. ioc->name, __func__);
  656. ret = -EAGAIN;
  657. goto out;
  658. }
  659. }
  660. ret = 0;
  661. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  662. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  663. request = mpt2sas_base_get_msg_frame(ioc, smid);
  664. memcpy(request, mpi_request, karg.data_sge_offset*4);
  665. ioc->ctl_cmds.smid = smid;
  666. data_out_sz = karg.data_out_size;
  667. data_in_sz = karg.data_in_size;
  668. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  669. mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  670. if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
  671. le16_to_cpu(mpi_request->FunctionDependent1) >
  672. ioc->facts.MaxDevHandle) {
  673. ret = -EINVAL;
  674. mpt2sas_base_free_smid(ioc, smid);
  675. goto out;
  676. }
  677. }
  678. /* obtain dma-able memory for data transfer */
  679. if (data_out_sz) /* WRITE */ {
  680. data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
  681. &data_out_dma);
  682. if (!data_out) {
  683. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  684. __LINE__, __func__);
  685. ret = -ENOMEM;
  686. mpt2sas_base_free_smid(ioc, smid);
  687. goto out;
  688. }
  689. if (copy_from_user(data_out, karg.data_out_buf_ptr,
  690. data_out_sz)) {
  691. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  692. __LINE__, __func__);
  693. ret = -EFAULT;
  694. mpt2sas_base_free_smid(ioc, smid);
  695. goto out;
  696. }
  697. }
  698. if (data_in_sz) /* READ */ {
  699. data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
  700. &data_in_dma);
  701. if (!data_in) {
  702. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  703. __LINE__, __func__);
  704. ret = -ENOMEM;
  705. mpt2sas_base_free_smid(ioc, smid);
  706. goto out;
  707. }
  708. }
  709. /* add scatter gather elements */
  710. psge = (void *)request + (karg.data_sge_offset*4);
  711. if (!data_out_sz && !data_in_sz) {
  712. mpt2sas_base_build_zero_len_sge(ioc, psge);
  713. } else if (data_out_sz && data_in_sz) {
  714. /* WRITE sgel first */
  715. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  716. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  717. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  718. ioc->base_add_sg_single(psge, sgl_flags |
  719. data_out_sz, data_out_dma);
  720. /* incr sgel */
  721. psge += ioc->sge_size;
  722. /* READ sgel last */
  723. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  724. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  725. MPI2_SGE_FLAGS_END_OF_LIST);
  726. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  727. ioc->base_add_sg_single(psge, sgl_flags |
  728. data_in_sz, data_in_dma);
  729. } else if (data_out_sz) /* WRITE */ {
  730. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  731. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  732. MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  733. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  734. ioc->base_add_sg_single(psge, sgl_flags |
  735. data_out_sz, data_out_dma);
  736. } else if (data_in_sz) /* READ */ {
  737. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  738. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  739. MPI2_SGE_FLAGS_END_OF_LIST);
  740. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  741. ioc->base_add_sg_single(psge, sgl_flags |
  742. data_in_sz, data_in_dma);
  743. }
  744. /* send command to firmware */
  745. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  746. _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
  747. #endif
  748. init_completion(&ioc->ctl_cmds.done);
  749. switch (mpi_request->Function) {
  750. case MPI2_FUNCTION_SCSI_IO_REQUEST:
  751. case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  752. {
  753. Mpi2SCSIIORequest_t *scsiio_request =
  754. (Mpi2SCSIIORequest_t *)request;
  755. scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  756. scsiio_request->SenseBufferLowAddress =
  757. mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  758. memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
  759. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
  760. mpt2sas_base_put_smid_scsi_io(ioc, smid,
  761. le16_to_cpu(mpi_request->FunctionDependent1));
  762. else
  763. mpt2sas_base_put_smid_default(ioc, smid);
  764. break;
  765. }
  766. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  767. {
  768. Mpi2SCSITaskManagementRequest_t *tm_request =
  769. (Mpi2SCSITaskManagementRequest_t *)request;
  770. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
  771. "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
  772. le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
  773. if (tm_request->TaskType ==
  774. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
  775. tm_request->TaskType ==
  776. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
  777. if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
  778. mpt2sas_base_free_smid(ioc, smid);
  779. goto out;
  780. }
  781. }
  782. mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
  783. tm_request->DevHandle));
  784. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  785. break;
  786. }
  787. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  788. {
  789. Mpi2SmpPassthroughRequest_t *smp_request =
  790. (Mpi2SmpPassthroughRequest_t *)mpi_request;
  791. u8 *data;
  792. /* ioc determines which port to use */
  793. smp_request->PhysicalPort = 0xFF;
  794. if (smp_request->PassthroughFlags &
  795. MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
  796. data = (u8 *)&smp_request->SGL;
  797. else
  798. data = data_out;
  799. if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
  800. ioc->ioc_link_reset_in_progress = 1;
  801. ioc->ignore_loginfos = 1;
  802. }
  803. mpt2sas_base_put_smid_default(ioc, smid);
  804. break;
  805. }
  806. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  807. {
  808. Mpi2SasIoUnitControlRequest_t *sasiounit_request =
  809. (Mpi2SasIoUnitControlRequest_t *)mpi_request;
  810. if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
  811. || sasiounit_request->Operation ==
  812. MPI2_SAS_OP_PHY_LINK_RESET) {
  813. ioc->ioc_link_reset_in_progress = 1;
  814. ioc->ignore_loginfos = 1;
  815. }
  816. mpt2sas_base_put_smid_default(ioc, smid);
  817. break;
  818. }
  819. default:
  820. mpt2sas_base_put_smid_default(ioc, smid);
  821. break;
  822. }
  823. if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
  824. timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
  825. else
  826. timeout = karg.timeout;
  827. timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
  828. timeout*HZ);
  829. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  830. Mpi2SCSITaskManagementRequest_t *tm_request =
  831. (Mpi2SCSITaskManagementRequest_t *)mpi_request;
  832. mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
  833. tm_request->DevHandle));
  834. } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
  835. mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
  836. ioc->ioc_link_reset_in_progress) {
  837. ioc->ioc_link_reset_in_progress = 0;
  838. ioc->ignore_loginfos = 0;
  839. }
  840. if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
  841. printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
  842. __func__);
  843. _debug_dump_mf(mpi_request, karg.data_sge_offset);
  844. if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
  845. issue_reset = 1;
  846. goto issue_host_reset;
  847. }
  848. mpi_reply = ioc->ctl_cmds.reply;
  849. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  850. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  851. if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
  852. (ioc->logging_level & MPT_DEBUG_TM)) {
  853. Mpi2SCSITaskManagementReply_t *tm_reply =
  854. (Mpi2SCSITaskManagementReply_t *)mpi_reply;
  855. printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
  856. "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
  857. "TerminationCount(0x%08x)\n", ioc->name,
  858. le16_to_cpu(tm_reply->IOCStatus),
  859. le32_to_cpu(tm_reply->IOCLogInfo),
  860. le32_to_cpu(tm_reply->TerminationCount));
  861. }
  862. #endif
  863. /* copy out xdata to user */
  864. if (data_in_sz) {
  865. if (copy_to_user(karg.data_in_buf_ptr, data_in,
  866. data_in_sz)) {
  867. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  868. __LINE__, __func__);
  869. ret = -ENODATA;
  870. goto out;
  871. }
  872. }
  873. /* copy out reply message frame to user */
  874. if (karg.max_reply_bytes) {
  875. sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
  876. if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
  877. sz)) {
  878. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  879. __LINE__, __func__);
  880. ret = -ENODATA;
  881. goto out;
  882. }
  883. }
  884. /* copy out sense to user */
  885. if (karg.max_sense_bytes && (mpi_request->Function ==
  886. MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
  887. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
  888. sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
  889. if (copy_to_user(karg.sense_data_ptr,
  890. ioc->ctl_cmds.sense, sz)) {
  891. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  892. __LINE__, __func__);
  893. ret = -ENODATA;
  894. goto out;
  895. }
  896. }
  897. issue_host_reset:
  898. if (issue_reset) {
  899. ret = -ENODATA;
  900. if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  901. mpi_request->Function ==
  902. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
  903. printk(MPT2SAS_INFO_FMT "issue target reset: handle "
  904. "= (0x%04x)\n", ioc->name,
  905. le16_to_cpu(mpi_request->FunctionDependent1));
  906. mpt2sas_halt_firmware(ioc);
  907. mpt2sas_scsih_issue_tm(ioc,
  908. le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
  909. 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
  910. 0, TM_MUTEX_ON);
  911. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  912. } else
  913. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  914. FORCE_BIG_HAMMER);
  915. }
  916. out:
  917. /* free memory associated with sg buffers */
  918. if (data_in)
  919. pci_free_consistent(ioc->pdev, data_in_sz, data_in,
  920. data_in_dma);
  921. if (data_out)
  922. pci_free_consistent(ioc->pdev, data_out_sz, data_out,
  923. data_out_dma);
  924. kfree(mpi_request);
  925. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  926. mutex_unlock(&ioc->ctl_cmds.mutex);
  927. return ret;
  928. }
  929. /**
  930. * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
  931. * @arg - user space buffer containing ioctl content
  932. */
  933. static long
  934. _ctl_getiocinfo(void __user *arg)
  935. {
  936. struct mpt2_ioctl_iocinfo karg;
  937. struct MPT2SAS_ADAPTER *ioc;
  938. if (copy_from_user(&karg, arg, sizeof(karg))) {
  939. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  940. __FILE__, __LINE__, __func__);
  941. return -EFAULT;
  942. }
  943. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  944. return -ENODEV;
  945. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  946. __func__));
  947. memset(&karg, 0 , sizeof(karg));
  948. if (ioc->is_warpdrive)
  949. karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
  950. else
  951. karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
  952. if (ioc->pfacts)
  953. karg.port_number = ioc->pfacts[0].PortNumber;
  954. karg.hw_rev = ioc->pdev->revision;
  955. karg.pci_id = ioc->pdev->device;
  956. karg.subsystem_device = ioc->pdev->subsystem_device;
  957. karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
  958. karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
  959. karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
  960. karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
  961. karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
  962. karg.firmware_version = ioc->facts.FWVersion.Word;
  963. strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
  964. strcat(karg.driver_version, "-");
  965. strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
  966. karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  967. if (copy_to_user(arg, &karg, sizeof(karg))) {
  968. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  969. __FILE__, __LINE__, __func__);
  970. return -EFAULT;
  971. }
  972. return 0;
  973. }
  974. /**
  975. * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
  976. * @arg - user space buffer containing ioctl content
  977. */
  978. static long
  979. _ctl_eventquery(void __user *arg)
  980. {
  981. struct mpt2_ioctl_eventquery karg;
  982. struct MPT2SAS_ADAPTER *ioc;
  983. if (copy_from_user(&karg, arg, sizeof(karg))) {
  984. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  985. __FILE__, __LINE__, __func__);
  986. return -EFAULT;
  987. }
  988. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  989. return -ENODEV;
  990. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  991. __func__));
  992. karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
  993. memcpy(karg.event_types, ioc->event_type,
  994. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
  995. if (copy_to_user(arg, &karg, sizeof(karg))) {
  996. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  997. __FILE__, __LINE__, __func__);
  998. return -EFAULT;
  999. }
  1000. return 0;
  1001. }
  1002. /**
  1003. * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
  1004. * @arg - user space buffer containing ioctl content
  1005. */
  1006. static long
  1007. _ctl_eventenable(void __user *arg)
  1008. {
  1009. struct mpt2_ioctl_eventenable karg;
  1010. struct MPT2SAS_ADAPTER *ioc;
  1011. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1012. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1013. __FILE__, __LINE__, __func__);
  1014. return -EFAULT;
  1015. }
  1016. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1017. return -ENODEV;
  1018. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  1019. __func__));
  1020. if (ioc->event_log)
  1021. return 0;
  1022. memcpy(ioc->event_type, karg.event_types,
  1023. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
  1024. mpt2sas_base_validate_event_type(ioc, ioc->event_type);
  1025. /* initialize event_log */
  1026. ioc->event_context = 0;
  1027. ioc->aen_event_read_flag = 0;
  1028. ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
  1029. sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
  1030. if (!ioc->event_log) {
  1031. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1032. __FILE__, __LINE__, __func__);
  1033. return -ENOMEM;
  1034. }
  1035. return 0;
  1036. }
  1037. /**
  1038. * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
  1039. * @arg - user space buffer containing ioctl content
  1040. */
  1041. static long
  1042. _ctl_eventreport(void __user *arg)
  1043. {
  1044. struct mpt2_ioctl_eventreport karg;
  1045. struct MPT2SAS_ADAPTER *ioc;
  1046. u32 number_bytes, max_events, max;
  1047. struct mpt2_ioctl_eventreport __user *uarg = arg;
  1048. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1049. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1050. __FILE__, __LINE__, __func__);
  1051. return -EFAULT;
  1052. }
  1053. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1054. return -ENODEV;
  1055. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  1056. __func__));
  1057. number_bytes = karg.hdr.max_data_size -
  1058. sizeof(struct mpt2_ioctl_header);
  1059. max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
  1060. max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
  1061. /* If fewer than 1 event is requested, there must have
  1062. * been some type of error.
  1063. */
  1064. if (!max || !ioc->event_log)
  1065. return -ENODATA;
  1066. number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
  1067. if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
  1068. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1069. __FILE__, __LINE__, __func__);
  1070. return -EFAULT;
  1071. }
  1072. /* reset flag so SIGIO can restart */
  1073. ioc->aen_event_read_flag = 0;
  1074. return 0;
  1075. }
  1076. /**
  1077. * _ctl_do_reset - main handler for MPT2HARDRESET opcode
  1078. * @arg - user space buffer containing ioctl content
  1079. */
  1080. static long
  1081. _ctl_do_reset(void __user *arg)
  1082. {
  1083. struct mpt2_ioctl_diag_reset karg;
  1084. struct MPT2SAS_ADAPTER *ioc;
  1085. int retval;
  1086. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1087. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1088. __FILE__, __LINE__, __func__);
  1089. return -EFAULT;
  1090. }
  1091. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1092. return -ENODEV;
  1093. if (ioc->shost_recovery || ioc->pci_error_recovery ||
  1094. ioc->is_driver_loading)
  1095. return -EAGAIN;
  1096. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  1097. __func__));
  1098. retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1099. FORCE_BIG_HAMMER);
  1100. printk(MPT2SAS_INFO_FMT "host reset: %s\n",
  1101. ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
  1102. return 0;
  1103. }
  1104. /**
  1105. * _ctl_btdh_search_sas_device - searching for sas device
  1106. * @ioc: per adapter object
  1107. * @btdh: btdh ioctl payload
  1108. */
  1109. static int
  1110. _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
  1111. struct mpt2_ioctl_btdh_mapping *btdh)
  1112. {
  1113. struct _sas_device *sas_device;
  1114. unsigned long flags;
  1115. int rc = 0;
  1116. if (list_empty(&ioc->sas_device_list))
  1117. return rc;
  1118. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1119. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  1120. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1121. btdh->handle == sas_device->handle) {
  1122. btdh->bus = sas_device->channel;
  1123. btdh->id = sas_device->id;
  1124. rc = 1;
  1125. goto out;
  1126. } else if (btdh->bus == sas_device->channel && btdh->id ==
  1127. sas_device->id && btdh->handle == 0xFFFF) {
  1128. btdh->handle = sas_device->handle;
  1129. rc = 1;
  1130. goto out;
  1131. }
  1132. }
  1133. out:
  1134. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1135. return rc;
  1136. }
  1137. /**
  1138. * _ctl_btdh_search_raid_device - searching for raid device
  1139. * @ioc: per adapter object
  1140. * @btdh: btdh ioctl payload
  1141. */
  1142. static int
  1143. _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
  1144. struct mpt2_ioctl_btdh_mapping *btdh)
  1145. {
  1146. struct _raid_device *raid_device;
  1147. unsigned long flags;
  1148. int rc = 0;
  1149. if (list_empty(&ioc->raid_device_list))
  1150. return rc;
  1151. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1152. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  1153. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1154. btdh->handle == raid_device->handle) {
  1155. btdh->bus = raid_device->channel;
  1156. btdh->id = raid_device->id;
  1157. rc = 1;
  1158. goto out;
  1159. } else if (btdh->bus == raid_device->channel && btdh->id ==
  1160. raid_device->id && btdh->handle == 0xFFFF) {
  1161. btdh->handle = raid_device->handle;
  1162. rc = 1;
  1163. goto out;
  1164. }
  1165. }
  1166. out:
  1167. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1168. return rc;
  1169. }
  1170. /**
  1171. * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
  1172. * @arg - user space buffer containing ioctl content
  1173. */
  1174. static long
  1175. _ctl_btdh_mapping(void __user *arg)
  1176. {
  1177. struct mpt2_ioctl_btdh_mapping karg;
  1178. struct MPT2SAS_ADAPTER *ioc;
  1179. int rc;
  1180. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1181. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1182. __FILE__, __LINE__, __func__);
  1183. return -EFAULT;
  1184. }
  1185. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1186. return -ENODEV;
  1187. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1188. __func__));
  1189. rc = _ctl_btdh_search_sas_device(ioc, &karg);
  1190. if (!rc)
  1191. _ctl_btdh_search_raid_device(ioc, &karg);
  1192. if (copy_to_user(arg, &karg, sizeof(karg))) {
  1193. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1194. __FILE__, __LINE__, __func__);
  1195. return -EFAULT;
  1196. }
  1197. return 0;
  1198. }
  1199. /**
  1200. * _ctl_diag_capability - return diag buffer capability
  1201. * @ioc: per adapter object
  1202. * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
  1203. *
  1204. * returns 1 when diag buffer support is enabled in firmware
  1205. */
  1206. static u8
  1207. _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
  1208. {
  1209. u8 rc = 0;
  1210. switch (buffer_type) {
  1211. case MPI2_DIAG_BUF_TYPE_TRACE:
  1212. if (ioc->facts.IOCCapabilities &
  1213. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
  1214. rc = 1;
  1215. break;
  1216. case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
  1217. if (ioc->facts.IOCCapabilities &
  1218. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
  1219. rc = 1;
  1220. break;
  1221. case MPI2_DIAG_BUF_TYPE_EXTENDED:
  1222. if (ioc->facts.IOCCapabilities &
  1223. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
  1224. rc = 1;
  1225. }
  1226. return rc;
  1227. }
  1228. /**
  1229. * _ctl_diag_register_2 - wrapper for registering diag buffer support
  1230. * @ioc: per adapter object
  1231. * @diag_register: the diag_register struct passed in from user space
  1232. *
  1233. */
  1234. static long
  1235. _ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
  1236. struct mpt2_diag_register *diag_register)
  1237. {
  1238. int rc, i;
  1239. void *request_data = NULL;
  1240. dma_addr_t request_data_dma;
  1241. u32 request_data_sz = 0;
  1242. Mpi2DiagBufferPostRequest_t *mpi_request;
  1243. Mpi2DiagBufferPostReply_t *mpi_reply;
  1244. u8 buffer_type;
  1245. unsigned long timeleft;
  1246. u16 smid;
  1247. u16 ioc_status;
  1248. u8 issue_reset = 0;
  1249. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1250. __func__));
  1251. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  1252. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  1253. ioc->name, __func__);
  1254. rc = -EAGAIN;
  1255. goto out;
  1256. }
  1257. buffer_type = diag_register->buffer_type;
  1258. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1259. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1260. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1261. return -EPERM;
  1262. }
  1263. if (ioc->diag_buffer_status[buffer_type] &
  1264. MPT2_DIAG_BUFFER_IS_REGISTERED) {
  1265. printk(MPT2SAS_ERR_FMT "%s: already has a registered "
  1266. "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
  1267. buffer_type);
  1268. return -EINVAL;
  1269. }
  1270. if (diag_register->requested_buffer_size % 4) {
  1271. printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
  1272. "is not 4 byte aligned\n", ioc->name, __func__);
  1273. return -EINVAL;
  1274. }
  1275. smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1276. if (!smid) {
  1277. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1278. ioc->name, __func__);
  1279. rc = -EAGAIN;
  1280. goto out;
  1281. }
  1282. rc = 0;
  1283. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  1284. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1285. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1286. ioc->ctl_cmds.smid = smid;
  1287. request_data = ioc->diag_buffer[buffer_type];
  1288. request_data_sz = diag_register->requested_buffer_size;
  1289. ioc->unique_id[buffer_type] = diag_register->unique_id;
  1290. ioc->diag_buffer_status[buffer_type] = 0;
  1291. memcpy(ioc->product_specific[buffer_type],
  1292. diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
  1293. ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
  1294. if (request_data) {
  1295. request_data_dma = ioc->diag_buffer_dma[buffer_type];
  1296. if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
  1297. pci_free_consistent(ioc->pdev,
  1298. ioc->diag_buffer_sz[buffer_type],
  1299. request_data, request_data_dma);
  1300. request_data = NULL;
  1301. }
  1302. }
  1303. if (request_data == NULL) {
  1304. ioc->diag_buffer_sz[buffer_type] = 0;
  1305. ioc->diag_buffer_dma[buffer_type] = 0;
  1306. request_data = pci_alloc_consistent(
  1307. ioc->pdev, request_data_sz, &request_data_dma);
  1308. if (request_data == NULL) {
  1309. printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
  1310. " for diag buffers, requested size(%d)\n",
  1311. ioc->name, __func__, request_data_sz);
  1312. mpt2sas_base_free_smid(ioc, smid);
  1313. return -ENOMEM;
  1314. }
  1315. ioc->diag_buffer[buffer_type] = request_data;
  1316. ioc->diag_buffer_sz[buffer_type] = request_data_sz;
  1317. ioc->diag_buffer_dma[buffer_type] = request_data_dma;
  1318. }
  1319. mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
  1320. mpi_request->BufferType = diag_register->buffer_type;
  1321. mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
  1322. mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
  1323. mpi_request->BufferLength = cpu_to_le32(request_data_sz);
  1324. mpi_request->VF_ID = 0; /* TODO */
  1325. mpi_request->VP_ID = 0;
  1326. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
  1327. "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
  1328. (unsigned long long)request_data_dma,
  1329. le32_to_cpu(mpi_request->BufferLength)));
  1330. for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
  1331. mpi_request->ProductSpecific[i] =
  1332. cpu_to_le32(ioc->product_specific[buffer_type][i]);
  1333. init_completion(&ioc->ctl_cmds.done);
  1334. mpt2sas_base_put_smid_default(ioc, smid);
  1335. timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1336. MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
  1337. if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
  1338. printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
  1339. __func__);
  1340. _debug_dump_mf(mpi_request,
  1341. sizeof(Mpi2DiagBufferPostRequest_t)/4);
  1342. if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
  1343. issue_reset = 1;
  1344. goto issue_host_reset;
  1345. }
  1346. /* process the completed Reply Message Frame */
  1347. if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
  1348. printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
  1349. ioc->name, __func__);
  1350. rc = -EFAULT;
  1351. goto out;
  1352. }
  1353. mpi_reply = ioc->ctl_cmds.reply;
  1354. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1355. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1356. ioc->diag_buffer_status[buffer_type] |=
  1357. MPT2_DIAG_BUFFER_IS_REGISTERED;
  1358. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
  1359. ioc->name, __func__));
  1360. } else {
  1361. printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
  1362. "log_info(0x%08x)\n", ioc->name, __func__,
  1363. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1364. rc = -EFAULT;
  1365. }
  1366. issue_host_reset:
  1367. if (issue_reset)
  1368. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1369. FORCE_BIG_HAMMER);
  1370. out:
  1371. if (rc && request_data)
  1372. pci_free_consistent(ioc->pdev, request_data_sz,
  1373. request_data, request_data_dma);
  1374. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  1375. return rc;
  1376. }
  1377. /**
  1378. * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
  1379. * @ioc: per adapter object
  1380. * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
  1381. *
  1382. * This is called when command line option diag_buffer_enable is enabled
  1383. * at driver load time.
  1384. */
  1385. void
  1386. mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
  1387. {
  1388. struct mpt2_diag_register diag_register;
  1389. memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
  1390. if (bits_to_register & 1) {
  1391. printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
  1392. ioc->name);
  1393. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  1394. /* register for 1MB buffers */
  1395. diag_register.requested_buffer_size = (1024 * 1024);
  1396. diag_register.unique_id = 0x7075900;
  1397. _ctl_diag_register_2(ioc, &diag_register);
  1398. }
  1399. if (bits_to_register & 2) {
  1400. printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
  1401. ioc->name);
  1402. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
  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. if (bits_to_register & 4) {
  1409. printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
  1410. ioc->name);
  1411. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
  1412. /* register for 2MB buffers */
  1413. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1414. diag_register.unique_id = 0x7075901;
  1415. _ctl_diag_register_2(ioc, &diag_register);
  1416. }
  1417. }
  1418. /**
  1419. * _ctl_diag_register - application register with driver
  1420. * @arg - user space buffer containing ioctl content
  1421. * @state - NON_BLOCKING or BLOCKING
  1422. *
  1423. * This will allow the driver to setup any required buffers that will be
  1424. * needed by firmware to communicate with the driver.
  1425. */
  1426. static long
  1427. _ctl_diag_register(void __user *arg, enum block_state state)
  1428. {
  1429. struct mpt2_diag_register karg;
  1430. struct MPT2SAS_ADAPTER *ioc;
  1431. long rc;
  1432. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1433. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1434. __FILE__, __LINE__, __func__);
  1435. return -EFAULT;
  1436. }
  1437. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1438. return -ENODEV;
  1439. if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
  1440. return -EAGAIN;
  1441. else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
  1442. return -ERESTARTSYS;
  1443. rc = _ctl_diag_register_2(ioc, &karg);
  1444. mutex_unlock(&ioc->ctl_cmds.mutex);
  1445. return rc;
  1446. }
  1447. /**
  1448. * _ctl_diag_unregister - application unregister with driver
  1449. * @arg - user space buffer containing ioctl content
  1450. *
  1451. * This will allow the driver to cleanup any memory allocated for diag
  1452. * messages and to free up any resources.
  1453. */
  1454. static long
  1455. _ctl_diag_unregister(void __user *arg)
  1456. {
  1457. struct mpt2_diag_unregister karg;
  1458. struct MPT2SAS_ADAPTER *ioc;
  1459. void *request_data;
  1460. dma_addr_t request_data_dma;
  1461. u32 request_data_sz;
  1462. u8 buffer_type;
  1463. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1464. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1465. __FILE__, __LINE__, __func__);
  1466. return -EFAULT;
  1467. }
  1468. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1469. return -ENODEV;
  1470. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1471. __func__));
  1472. buffer_type = karg.unique_id & 0x000000ff;
  1473. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1474. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1475. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1476. return -EPERM;
  1477. }
  1478. if ((ioc->diag_buffer_status[buffer_type] &
  1479. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1480. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
  1481. "registered\n", ioc->name, __func__, buffer_type);
  1482. return -EINVAL;
  1483. }
  1484. if ((ioc->diag_buffer_status[buffer_type] &
  1485. MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
  1486. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
  1487. "released\n", ioc->name, __func__, buffer_type);
  1488. return -EINVAL;
  1489. }
  1490. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1491. printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
  1492. "registered\n", ioc->name, __func__, karg.unique_id);
  1493. return -EINVAL;
  1494. }
  1495. request_data = ioc->diag_buffer[buffer_type];
  1496. if (!request_data) {
  1497. printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
  1498. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1499. return -ENOMEM;
  1500. }
  1501. request_data_sz = ioc->diag_buffer_sz[buffer_type];
  1502. request_data_dma = ioc->diag_buffer_dma[buffer_type];
  1503. pci_free_consistent(ioc->pdev, request_data_sz,
  1504. request_data, request_data_dma);
  1505. ioc->diag_buffer[buffer_type] = NULL;
  1506. ioc->diag_buffer_status[buffer_type] = 0;
  1507. return 0;
  1508. }
  1509. /**
  1510. * _ctl_diag_query - query relevant info associated with diag buffers
  1511. * @arg - user space buffer containing ioctl content
  1512. *
  1513. * The application will send only buffer_type and unique_id. Driver will
  1514. * inspect unique_id first, if valid, fill in all the info. If unique_id is
  1515. * 0x00, the driver will return info specified by Buffer Type.
  1516. */
  1517. static long
  1518. _ctl_diag_query(void __user *arg)
  1519. {
  1520. struct mpt2_diag_query karg;
  1521. struct MPT2SAS_ADAPTER *ioc;
  1522. void *request_data;
  1523. int i;
  1524. u8 buffer_type;
  1525. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1526. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1527. __FILE__, __LINE__, __func__);
  1528. return -EFAULT;
  1529. }
  1530. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1531. return -ENODEV;
  1532. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1533. __func__));
  1534. karg.application_flags = 0;
  1535. buffer_type = karg.buffer_type;
  1536. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1537. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1538. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1539. return -EPERM;
  1540. }
  1541. if ((ioc->diag_buffer_status[buffer_type] &
  1542. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1543. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
  1544. "registered\n", ioc->name, __func__, buffer_type);
  1545. return -EINVAL;
  1546. }
  1547. if (karg.unique_id & 0xffffff00) {
  1548. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1549. printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
  1550. "registered\n", ioc->name, __func__,
  1551. karg.unique_id);
  1552. return -EINVAL;
  1553. }
  1554. }
  1555. request_data = ioc->diag_buffer[buffer_type];
  1556. if (!request_data) {
  1557. printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
  1558. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1559. return -ENOMEM;
  1560. }
  1561. if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
  1562. karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
  1563. MPT2_APP_FLAGS_BUFFER_VALID);
  1564. else
  1565. karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
  1566. MPT2_APP_FLAGS_BUFFER_VALID |
  1567. MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
  1568. for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
  1569. karg.product_specific[i] =
  1570. ioc->product_specific[buffer_type][i];
  1571. karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
  1572. karg.driver_added_buffer_size = 0;
  1573. karg.unique_id = ioc->unique_id[buffer_type];
  1574. karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
  1575. if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
  1576. printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
  1577. "data @ %p\n", ioc->name, __func__, arg);
  1578. return -EFAULT;
  1579. }
  1580. return 0;
  1581. }
  1582. /**
  1583. * _ctl_send_release - Diag Release Message
  1584. * @ioc: per adapter object
  1585. * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
  1586. * @issue_reset - specifies whether host reset is required.
  1587. *
  1588. */
  1589. static int
  1590. _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
  1591. {
  1592. Mpi2DiagReleaseRequest_t *mpi_request;
  1593. Mpi2DiagReleaseReply_t *mpi_reply;
  1594. u16 smid;
  1595. u16 ioc_status;
  1596. u32 ioc_state;
  1597. int rc;
  1598. unsigned long timeleft;
  1599. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1600. __func__));
  1601. rc = 0;
  1602. *issue_reset = 0;
  1603. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1604. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1605. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  1606. "skipping due to FAULT state\n", ioc->name,
  1607. __func__));
  1608. rc = -EAGAIN;
  1609. goto out;
  1610. }
  1611. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  1612. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  1613. ioc->name, __func__);
  1614. rc = -EAGAIN;
  1615. goto out;
  1616. }
  1617. smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1618. if (!smid) {
  1619. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1620. ioc->name, __func__);
  1621. rc = -EAGAIN;
  1622. goto out;
  1623. }
  1624. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  1625. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1626. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1627. ioc->ctl_cmds.smid = smid;
  1628. mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
  1629. mpi_request->BufferType = buffer_type;
  1630. mpi_request->VF_ID = 0; /* TODO */
  1631. mpi_request->VP_ID = 0;
  1632. init_completion(&ioc->ctl_cmds.done);
  1633. mpt2sas_base_put_smid_default(ioc, smid);
  1634. timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1635. MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
  1636. if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
  1637. printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
  1638. __func__);
  1639. _debug_dump_mf(mpi_request,
  1640. sizeof(Mpi2DiagReleaseRequest_t)/4);
  1641. if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
  1642. *issue_reset = 1;
  1643. rc = -EFAULT;
  1644. goto out;
  1645. }
  1646. /* process the completed Reply Message Frame */
  1647. if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
  1648. printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
  1649. ioc->name, __func__);
  1650. rc = -EFAULT;
  1651. goto out;
  1652. }
  1653. mpi_reply = ioc->ctl_cmds.reply;
  1654. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1655. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1656. ioc->diag_buffer_status[buffer_type] |=
  1657. MPT2_DIAG_BUFFER_IS_RELEASED;
  1658. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
  1659. ioc->name, __func__));
  1660. } else {
  1661. printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
  1662. "log_info(0x%08x)\n", ioc->name, __func__,
  1663. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1664. rc = -EFAULT;
  1665. }
  1666. out:
  1667. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  1668. return rc;
  1669. }
  1670. /**
  1671. * _ctl_diag_release - request to send Diag Release Message to firmware
  1672. * @arg - user space buffer containing ioctl content
  1673. * @state - NON_BLOCKING or BLOCKING
  1674. *
  1675. * This allows ownership of the specified buffer to returned to the driver,
  1676. * allowing an application to read the buffer without fear that firmware is
  1677. * overwritting information in the buffer.
  1678. */
  1679. static long
  1680. _ctl_diag_release(void __user *arg, enum block_state state)
  1681. {
  1682. struct mpt2_diag_release karg;
  1683. struct MPT2SAS_ADAPTER *ioc;
  1684. void *request_data;
  1685. int rc;
  1686. u8 buffer_type;
  1687. u8 issue_reset = 0;
  1688. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1689. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1690. __FILE__, __LINE__, __func__);
  1691. return -EFAULT;
  1692. }
  1693. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1694. return -ENODEV;
  1695. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1696. __func__));
  1697. buffer_type = karg.unique_id & 0x000000ff;
  1698. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1699. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1700. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1701. return -EPERM;
  1702. }
  1703. if ((ioc->diag_buffer_status[buffer_type] &
  1704. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1705. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
  1706. "registered\n", ioc->name, __func__, buffer_type);
  1707. return -EINVAL;
  1708. }
  1709. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1710. printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
  1711. "registered\n", ioc->name, __func__, karg.unique_id);
  1712. return -EINVAL;
  1713. }
  1714. if (ioc->diag_buffer_status[buffer_type] &
  1715. MPT2_DIAG_BUFFER_IS_RELEASED) {
  1716. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
  1717. "is already released\n", ioc->name, __func__,
  1718. buffer_type);
  1719. return 0;
  1720. }
  1721. request_data = ioc->diag_buffer[buffer_type];
  1722. if (!request_data) {
  1723. printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
  1724. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1725. return -ENOMEM;
  1726. }
  1727. /* buffers were released by due to host reset */
  1728. if ((ioc->diag_buffer_status[buffer_type] &
  1729. MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
  1730. ioc->diag_buffer_status[buffer_type] |=
  1731. MPT2_DIAG_BUFFER_IS_RELEASED;
  1732. ioc->diag_buffer_status[buffer_type] &=
  1733. ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
  1734. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
  1735. "was released due to host reset\n", ioc->name, __func__,
  1736. buffer_type);
  1737. return 0;
  1738. }
  1739. if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
  1740. return -EAGAIN;
  1741. else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
  1742. return -ERESTARTSYS;
  1743. rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
  1744. if (issue_reset)
  1745. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1746. FORCE_BIG_HAMMER);
  1747. mutex_unlock(&ioc->ctl_cmds.mutex);
  1748. return rc;
  1749. }
  1750. /**
  1751. * _ctl_diag_read_buffer - request for copy of the diag buffer
  1752. * @arg - user space buffer containing ioctl content
  1753. * @state - NON_BLOCKING or BLOCKING
  1754. */
  1755. static long
  1756. _ctl_diag_read_buffer(void __user *arg, enum block_state state)
  1757. {
  1758. struct mpt2_diag_read_buffer karg;
  1759. struct mpt2_diag_read_buffer __user *uarg = arg;
  1760. struct MPT2SAS_ADAPTER *ioc;
  1761. void *request_data, *diag_data;
  1762. Mpi2DiagBufferPostRequest_t *mpi_request;
  1763. Mpi2DiagBufferPostReply_t *mpi_reply;
  1764. int rc, i;
  1765. u8 buffer_type;
  1766. unsigned long timeleft, request_size, copy_size;
  1767. u16 smid;
  1768. u16 ioc_status;
  1769. u8 issue_reset = 0;
  1770. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1771. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1772. __FILE__, __LINE__, __func__);
  1773. return -EFAULT;
  1774. }
  1775. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1776. return -ENODEV;
  1777. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1778. __func__));
  1779. buffer_type = karg.unique_id & 0x000000ff;
  1780. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1781. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1782. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1783. return -EPERM;
  1784. }
  1785. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1786. printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
  1787. "registered\n", ioc->name, __func__, karg.unique_id);
  1788. return -EINVAL;
  1789. }
  1790. request_data = ioc->diag_buffer[buffer_type];
  1791. if (!request_data) {
  1792. printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
  1793. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1794. return -ENOMEM;
  1795. }
  1796. request_size = ioc->diag_buffer_sz[buffer_type];
  1797. if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
  1798. printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
  1799. "or bytes_to_read are not 4 byte aligned\n", ioc->name,
  1800. __func__);
  1801. return -EINVAL;
  1802. }
  1803. if (karg.starting_offset > request_size)
  1804. return -EINVAL;
  1805. diag_data = (void *)(request_data + karg.starting_offset);
  1806. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
  1807. "offset(%d), sz(%d)\n", ioc->name, __func__,
  1808. diag_data, karg.starting_offset, karg.bytes_to_read));
  1809. /* Truncate data on requests that are too large */
  1810. if ((diag_data + karg.bytes_to_read < diag_data) ||
  1811. (diag_data + karg.bytes_to_read > request_data + request_size))
  1812. copy_size = request_size - karg.starting_offset;
  1813. else
  1814. copy_size = karg.bytes_to_read;
  1815. if (copy_to_user((void __user *)uarg->diagnostic_data,
  1816. diag_data, copy_size)) {
  1817. printk(MPT2SAS_ERR_FMT "%s: Unable to write "
  1818. "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
  1819. __func__, diag_data);
  1820. return -EFAULT;
  1821. }
  1822. if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
  1823. return 0;
  1824. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
  1825. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
  1826. if ((ioc->diag_buffer_status[buffer_type] &
  1827. MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
  1828. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  1829. "buffer_type(0x%02x) is still registered\n", ioc->name,
  1830. __func__, buffer_type));
  1831. return 0;
  1832. }
  1833. /* Get a free request frame and save the message context.
  1834. */
  1835. if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
  1836. return -EAGAIN;
  1837. else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
  1838. return -ERESTARTSYS;
  1839. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  1840. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  1841. ioc->name, __func__);
  1842. rc = -EAGAIN;
  1843. goto out;
  1844. }
  1845. smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1846. if (!smid) {
  1847. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1848. ioc->name, __func__);
  1849. rc = -EAGAIN;
  1850. goto out;
  1851. }
  1852. rc = 0;
  1853. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  1854. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1855. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1856. ioc->ctl_cmds.smid = smid;
  1857. mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
  1858. mpi_request->BufferType = buffer_type;
  1859. mpi_request->BufferLength =
  1860. cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
  1861. mpi_request->BufferAddress =
  1862. cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
  1863. for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
  1864. mpi_request->ProductSpecific[i] =
  1865. cpu_to_le32(ioc->product_specific[buffer_type][i]);
  1866. mpi_request->VF_ID = 0; /* TODO */
  1867. mpi_request->VP_ID = 0;
  1868. init_completion(&ioc->ctl_cmds.done);
  1869. mpt2sas_base_put_smid_default(ioc, smid);
  1870. timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1871. MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
  1872. if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
  1873. printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
  1874. __func__);
  1875. _debug_dump_mf(mpi_request,
  1876. sizeof(Mpi2DiagBufferPostRequest_t)/4);
  1877. if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
  1878. issue_reset = 1;
  1879. goto issue_host_reset;
  1880. }
  1881. /* process the completed Reply Message Frame */
  1882. if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
  1883. printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
  1884. ioc->name, __func__);
  1885. rc = -EFAULT;
  1886. goto out;
  1887. }
  1888. mpi_reply = ioc->ctl_cmds.reply;
  1889. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1890. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1891. ioc->diag_buffer_status[buffer_type] |=
  1892. MPT2_DIAG_BUFFER_IS_REGISTERED;
  1893. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
  1894. ioc->name, __func__));
  1895. } else {
  1896. printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
  1897. "log_info(0x%08x)\n", ioc->name, __func__,
  1898. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1899. rc = -EFAULT;
  1900. }
  1901. issue_host_reset:
  1902. if (issue_reset)
  1903. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1904. FORCE_BIG_HAMMER);
  1905. out:
  1906. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  1907. mutex_unlock(&ioc->ctl_cmds.mutex);
  1908. return rc;
  1909. }
  1910. /**
  1911. * _ctl_ioctl_main - main ioctl entry point
  1912. * @file - (struct file)
  1913. * @cmd - ioctl opcode
  1914. * @arg -
  1915. */
  1916. static long
  1917. _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
  1918. {
  1919. enum block_state state;
  1920. long ret = -EINVAL;
  1921. state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
  1922. BLOCKING;
  1923. switch (cmd) {
  1924. case MPT2IOCINFO:
  1925. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
  1926. ret = _ctl_getiocinfo(arg);
  1927. break;
  1928. case MPT2COMMAND:
  1929. {
  1930. struct mpt2_ioctl_command karg;
  1931. struct mpt2_ioctl_command __user *uarg;
  1932. struct MPT2SAS_ADAPTER *ioc;
  1933. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1934. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1935. __FILE__, __LINE__, __func__);
  1936. return -EFAULT;
  1937. }
  1938. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
  1939. !ioc)
  1940. return -ENODEV;
  1941. if (ioc->shost_recovery || ioc->pci_error_recovery ||
  1942. ioc->is_driver_loading)
  1943. return -EAGAIN;
  1944. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
  1945. uarg = arg;
  1946. ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
  1947. }
  1948. break;
  1949. }
  1950. case MPT2EVENTQUERY:
  1951. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
  1952. ret = _ctl_eventquery(arg);
  1953. break;
  1954. case MPT2EVENTENABLE:
  1955. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
  1956. ret = _ctl_eventenable(arg);
  1957. break;
  1958. case MPT2EVENTREPORT:
  1959. ret = _ctl_eventreport(arg);
  1960. break;
  1961. case MPT2HARDRESET:
  1962. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
  1963. ret = _ctl_do_reset(arg);
  1964. break;
  1965. case MPT2BTDHMAPPING:
  1966. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
  1967. ret = _ctl_btdh_mapping(arg);
  1968. break;
  1969. case MPT2DIAGREGISTER:
  1970. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
  1971. ret = _ctl_diag_register(arg, state);
  1972. break;
  1973. case MPT2DIAGUNREGISTER:
  1974. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
  1975. ret = _ctl_diag_unregister(arg);
  1976. break;
  1977. case MPT2DIAGQUERY:
  1978. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
  1979. ret = _ctl_diag_query(arg);
  1980. break;
  1981. case MPT2DIAGRELEASE:
  1982. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
  1983. ret = _ctl_diag_release(arg, state);
  1984. break;
  1985. case MPT2DIAGREADBUFFER:
  1986. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
  1987. ret = _ctl_diag_read_buffer(arg, state);
  1988. break;
  1989. default:
  1990. {
  1991. struct mpt2_ioctl_command karg;
  1992. struct MPT2SAS_ADAPTER *ioc;
  1993. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1994. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1995. __FILE__, __LINE__, __func__);
  1996. return -EFAULT;
  1997. }
  1998. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
  1999. !ioc)
  2000. return -ENODEV;
  2001. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
  2002. "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
  2003. break;
  2004. }
  2005. }
  2006. return ret;
  2007. }
  2008. /**
  2009. * _ctl_ioctl - main ioctl entry point (unlocked)
  2010. * @file - (struct file)
  2011. * @cmd - ioctl opcode
  2012. * @arg -
  2013. */
  2014. static long
  2015. _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2016. {
  2017. long ret;
  2018. mutex_lock(&_ctl_mutex);
  2019. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
  2020. mutex_unlock(&_ctl_mutex);
  2021. return ret;
  2022. }
  2023. #ifdef CONFIG_COMPAT
  2024. /**
  2025. * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
  2026. * @file - (struct file)
  2027. * @cmd - ioctl opcode
  2028. * @arg - (struct mpt2_ioctl_command32)
  2029. *
  2030. * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
  2031. */
  2032. static long
  2033. _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
  2034. {
  2035. struct mpt2_ioctl_command32 karg32;
  2036. struct mpt2_ioctl_command32 __user *uarg;
  2037. struct mpt2_ioctl_command karg;
  2038. struct MPT2SAS_ADAPTER *ioc;
  2039. enum block_state state;
  2040. if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
  2041. return -EINVAL;
  2042. uarg = (struct mpt2_ioctl_command32 __user *) arg;
  2043. if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
  2044. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  2045. __FILE__, __LINE__, __func__);
  2046. return -EFAULT;
  2047. }
  2048. if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
  2049. return -ENODEV;
  2050. if (ioc->shost_recovery || ioc->pci_error_recovery ||
  2051. ioc->is_driver_loading)
  2052. return -EAGAIN;
  2053. memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
  2054. karg.hdr.ioc_number = karg32.hdr.ioc_number;
  2055. karg.hdr.port_number = karg32.hdr.port_number;
  2056. karg.hdr.max_data_size = karg32.hdr.max_data_size;
  2057. karg.timeout = karg32.timeout;
  2058. karg.max_reply_bytes = karg32.max_reply_bytes;
  2059. karg.data_in_size = karg32.data_in_size;
  2060. karg.data_out_size = karg32.data_out_size;
  2061. karg.max_sense_bytes = karg32.max_sense_bytes;
  2062. karg.data_sge_offset = karg32.data_sge_offset;
  2063. karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
  2064. karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
  2065. karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
  2066. karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
  2067. state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
  2068. return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
  2069. }
  2070. /**
  2071. * _ctl_ioctl_compat - main ioctl entry point (compat)
  2072. * @file -
  2073. * @cmd -
  2074. * @arg -
  2075. *
  2076. * This routine handles 32 bit applications in 64bit os.
  2077. */
  2078. static long
  2079. _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
  2080. {
  2081. long ret;
  2082. mutex_lock(&_ctl_mutex);
  2083. if (cmd == MPT2COMMAND32)
  2084. ret = _ctl_compat_mpt_command(file, cmd, arg);
  2085. else
  2086. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
  2087. mutex_unlock(&_ctl_mutex);
  2088. return ret;
  2089. }
  2090. #endif
  2091. /* scsi host attributes */
  2092. /**
  2093. * _ctl_version_fw_show - firmware version
  2094. * @cdev - pointer to embedded class device
  2095. * @buf - the buffer returned
  2096. *
  2097. * A sysfs 'read-only' shost attribute.
  2098. */
  2099. static ssize_t
  2100. _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
  2101. char *buf)
  2102. {
  2103. struct Scsi_Host *shost = class_to_shost(cdev);
  2104. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2105. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2106. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  2107. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  2108. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  2109. ioc->facts.FWVersion.Word & 0x000000FF);
  2110. }
  2111. static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
  2112. /**
  2113. * _ctl_version_bios_show - bios version
  2114. * @cdev - pointer to embedded class device
  2115. * @buf - the buffer returned
  2116. *
  2117. * A sysfs 'read-only' shost attribute.
  2118. */
  2119. static ssize_t
  2120. _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
  2121. char *buf)
  2122. {
  2123. struct Scsi_Host *shost = class_to_shost(cdev);
  2124. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2125. u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  2126. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2127. (version & 0xFF000000) >> 24,
  2128. (version & 0x00FF0000) >> 16,
  2129. (version & 0x0000FF00) >> 8,
  2130. version & 0x000000FF);
  2131. }
  2132. static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
  2133. /**
  2134. * _ctl_version_mpi_show - MPI (message passing interface) version
  2135. * @cdev - pointer to embedded class device
  2136. * @buf - the buffer returned
  2137. *
  2138. * A sysfs 'read-only' shost attribute.
  2139. */
  2140. static ssize_t
  2141. _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
  2142. char *buf)
  2143. {
  2144. struct Scsi_Host *shost = class_to_shost(cdev);
  2145. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2146. return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
  2147. ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
  2148. }
  2149. static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
  2150. /**
  2151. * _ctl_version_product_show - product name
  2152. * @cdev - pointer to embedded class device
  2153. * @buf - the buffer returned
  2154. *
  2155. * A sysfs 'read-only' shost attribute.
  2156. */
  2157. static ssize_t
  2158. _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
  2159. char *buf)
  2160. {
  2161. struct Scsi_Host *shost = class_to_shost(cdev);
  2162. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2163. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
  2164. }
  2165. static DEVICE_ATTR(version_product, S_IRUGO,
  2166. _ctl_version_product_show, NULL);
  2167. /**
  2168. * _ctl_version_nvdata_persistent_show - ndvata persistent version
  2169. * @cdev - pointer to embedded class device
  2170. * @buf - the buffer returned
  2171. *
  2172. * A sysfs 'read-only' shost attribute.
  2173. */
  2174. static ssize_t
  2175. _ctl_version_nvdata_persistent_show(struct device *cdev,
  2176. struct device_attribute *attr, char *buf)
  2177. {
  2178. struct Scsi_Host *shost = class_to_shost(cdev);
  2179. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2180. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2181. le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
  2182. }
  2183. static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
  2184. _ctl_version_nvdata_persistent_show, NULL);
  2185. /**
  2186. * _ctl_version_nvdata_default_show - nvdata default version
  2187. * @cdev - pointer to embedded class device
  2188. * @buf - the buffer returned
  2189. *
  2190. * A sysfs 'read-only' shost attribute.
  2191. */
  2192. static ssize_t
  2193. _ctl_version_nvdata_default_show(struct device *cdev,
  2194. struct device_attribute *attr, char *buf)
  2195. {
  2196. struct Scsi_Host *shost = class_to_shost(cdev);
  2197. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2198. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2199. le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
  2200. }
  2201. static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
  2202. _ctl_version_nvdata_default_show, NULL);
  2203. /**
  2204. * _ctl_board_name_show - board name
  2205. * @cdev - pointer to embedded class device
  2206. * @buf - the buffer returned
  2207. *
  2208. * A sysfs 'read-only' shost attribute.
  2209. */
  2210. static ssize_t
  2211. _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
  2212. char *buf)
  2213. {
  2214. struct Scsi_Host *shost = class_to_shost(cdev);
  2215. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2216. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
  2217. }
  2218. static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
  2219. /**
  2220. * _ctl_board_assembly_show - board assembly name
  2221. * @cdev - pointer to embedded class device
  2222. * @buf - the buffer returned
  2223. *
  2224. * A sysfs 'read-only' shost attribute.
  2225. */
  2226. static ssize_t
  2227. _ctl_board_assembly_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, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
  2233. }
  2234. static DEVICE_ATTR(board_assembly, S_IRUGO,
  2235. _ctl_board_assembly_show, NULL);
  2236. /**
  2237. * _ctl_board_tracer_show - board tracer number
  2238. * @cdev - pointer to embedded class device
  2239. * @buf - the buffer returned
  2240. *
  2241. * A sysfs 'read-only' shost attribute.
  2242. */
  2243. static ssize_t
  2244. _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
  2245. char *buf)
  2246. {
  2247. struct Scsi_Host *shost = class_to_shost(cdev);
  2248. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2249. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
  2250. }
  2251. static DEVICE_ATTR(board_tracer, S_IRUGO,
  2252. _ctl_board_tracer_show, NULL);
  2253. /**
  2254. * _ctl_io_delay_show - io missing delay
  2255. * @cdev - pointer to embedded class device
  2256. * @buf - the buffer returned
  2257. *
  2258. * This is for firmware implemention for deboucing device
  2259. * removal events.
  2260. *
  2261. * A sysfs 'read-only' shost attribute.
  2262. */
  2263. static ssize_t
  2264. _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
  2265. char *buf)
  2266. {
  2267. struct Scsi_Host *shost = class_to_shost(cdev);
  2268. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2269. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
  2270. }
  2271. static DEVICE_ATTR(io_delay, S_IRUGO,
  2272. _ctl_io_delay_show, NULL);
  2273. /**
  2274. * _ctl_device_delay_show - device missing delay
  2275. * @cdev - pointer to embedded class device
  2276. * @buf - the buffer returned
  2277. *
  2278. * This is for firmware implemention for deboucing device
  2279. * removal events.
  2280. *
  2281. * A sysfs 'read-only' shost attribute.
  2282. */
  2283. static ssize_t
  2284. _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
  2285. char *buf)
  2286. {
  2287. struct Scsi_Host *shost = class_to_shost(cdev);
  2288. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2289. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
  2290. }
  2291. static DEVICE_ATTR(device_delay, S_IRUGO,
  2292. _ctl_device_delay_show, NULL);
  2293. /**
  2294. * _ctl_fw_queue_depth_show - global credits
  2295. * @cdev - pointer to embedded class device
  2296. * @buf - the buffer returned
  2297. *
  2298. * This is firmware queue depth limit
  2299. *
  2300. * A sysfs 'read-only' shost attribute.
  2301. */
  2302. static ssize_t
  2303. _ctl_fw_queue_depth_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, "%02d\n", ioc->facts.RequestCredit);
  2309. }
  2310. static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
  2311. _ctl_fw_queue_depth_show, NULL);
  2312. /**
  2313. * _ctl_sas_address_show - sas address
  2314. * @cdev - pointer to embedded class device
  2315. * @buf - the buffer returned
  2316. *
  2317. * This is the controller sas address
  2318. *
  2319. * A sysfs 'read-only' shost attribute.
  2320. */
  2321. static ssize_t
  2322. _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
  2323. char *buf)
  2324. {
  2325. struct Scsi_Host *shost = class_to_shost(cdev);
  2326. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2327. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  2328. (unsigned long long)ioc->sas_hba.sas_address);
  2329. }
  2330. static DEVICE_ATTR(host_sas_address, S_IRUGO,
  2331. _ctl_host_sas_address_show, NULL);
  2332. /**
  2333. * _ctl_logging_level_show - logging level
  2334. * @cdev - pointer to embedded class device
  2335. * @buf - the buffer returned
  2336. *
  2337. * A sysfs 'read/write' shost attribute.
  2338. */
  2339. static ssize_t
  2340. _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
  2341. char *buf)
  2342. {
  2343. struct Scsi_Host *shost = class_to_shost(cdev);
  2344. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2345. return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
  2346. }
  2347. static ssize_t
  2348. _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
  2349. const char *buf, size_t count)
  2350. {
  2351. struct Scsi_Host *shost = class_to_shost(cdev);
  2352. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2353. int val = 0;
  2354. if (sscanf(buf, "%x", &val) != 1)
  2355. return -EINVAL;
  2356. ioc->logging_level = val;
  2357. printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
  2358. ioc->logging_level);
  2359. return strlen(buf);
  2360. }
  2361. static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
  2362. _ctl_logging_level_show, _ctl_logging_level_store);
  2363. /* device attributes */
  2364. /*
  2365. * _ctl_fwfault_debug_show - show/store fwfault_debug
  2366. * @cdev - pointer to embedded class device
  2367. * @buf - the buffer returned
  2368. *
  2369. * mpt2sas_fwfault_debug is command line option
  2370. * A sysfs 'read/write' shost attribute.
  2371. */
  2372. static ssize_t
  2373. _ctl_fwfault_debug_show(struct device *cdev,
  2374. struct device_attribute *attr, char *buf)
  2375. {
  2376. struct Scsi_Host *shost = class_to_shost(cdev);
  2377. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2378. return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
  2379. }
  2380. static ssize_t
  2381. _ctl_fwfault_debug_store(struct device *cdev,
  2382. struct device_attribute *attr, const char *buf, size_t count)
  2383. {
  2384. struct Scsi_Host *shost = class_to_shost(cdev);
  2385. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2386. int val = 0;
  2387. if (sscanf(buf, "%d", &val) != 1)
  2388. return -EINVAL;
  2389. ioc->fwfault_debug = val;
  2390. printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
  2391. ioc->fwfault_debug);
  2392. return strlen(buf);
  2393. }
  2394. static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
  2395. _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
  2396. /**
  2397. * _ctl_ioc_reset_count_show - ioc reset count
  2398. * @cdev - pointer to embedded class device
  2399. * @buf - the buffer returned
  2400. *
  2401. * This is firmware queue depth limit
  2402. *
  2403. * A sysfs 'read-only' shost attribute.
  2404. */
  2405. static ssize_t
  2406. _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
  2407. char *buf)
  2408. {
  2409. struct Scsi_Host *shost = class_to_shost(cdev);
  2410. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2411. return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
  2412. }
  2413. static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
  2414. _ctl_ioc_reset_count_show, NULL);
  2415. /**
  2416. * _ctl_ioc_reply_queue_count_show - number of reply queues
  2417. * @cdev - pointer to embedded class device
  2418. * @buf - the buffer returned
  2419. *
  2420. * This is number of reply queues
  2421. *
  2422. * A sysfs 'read-only' shost attribute.
  2423. */
  2424. static ssize_t
  2425. _ctl_ioc_reply_queue_count_show(struct device *cdev,
  2426. struct device_attribute *attr, char *buf)
  2427. {
  2428. u8 reply_queue_count;
  2429. struct Scsi_Host *shost = class_to_shost(cdev);
  2430. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2431. if ((ioc->facts.IOCCapabilities &
  2432. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
  2433. reply_queue_count = ioc->reply_queue_count;
  2434. else
  2435. reply_queue_count = 1;
  2436. return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
  2437. }
  2438. static DEVICE_ATTR(reply_queue_count, S_IRUGO,
  2439. _ctl_ioc_reply_queue_count_show, NULL);
  2440. struct DIAG_BUFFER_START {
  2441. __le32 Size;
  2442. __le32 DiagVersion;
  2443. u8 BufferType;
  2444. u8 Reserved[3];
  2445. __le32 Reserved1;
  2446. __le32 Reserved2;
  2447. __le32 Reserved3;
  2448. };
  2449. /**
  2450. * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
  2451. * @cdev - pointer to embedded class device
  2452. * @buf - the buffer returned
  2453. *
  2454. * A sysfs 'read-only' shost attribute.
  2455. */
  2456. static ssize_t
  2457. _ctl_host_trace_buffer_size_show(struct device *cdev,
  2458. struct device_attribute *attr, char *buf)
  2459. {
  2460. struct Scsi_Host *shost = class_to_shost(cdev);
  2461. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2462. u32 size = 0;
  2463. struct DIAG_BUFFER_START *request_data;
  2464. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2465. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2466. "registered\n", ioc->name, __func__);
  2467. return 0;
  2468. }
  2469. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2470. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2471. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2472. "registered\n", ioc->name, __func__);
  2473. return 0;
  2474. }
  2475. request_data = (struct DIAG_BUFFER_START *)
  2476. ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
  2477. if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
  2478. le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
  2479. le32_to_cpu(request_data->Reserved3) == 0x4742444c)
  2480. size = le32_to_cpu(request_data->Size);
  2481. ioc->ring_buffer_sz = size;
  2482. return snprintf(buf, PAGE_SIZE, "%d\n", size);
  2483. }
  2484. static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
  2485. _ctl_host_trace_buffer_size_show, NULL);
  2486. /**
  2487. * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
  2488. * @cdev - pointer to embedded class device
  2489. * @buf - the buffer returned
  2490. *
  2491. * A sysfs 'read/write' shost attribute.
  2492. *
  2493. * You will only be able to read 4k bytes of ring buffer at a time.
  2494. * In order to read beyond 4k bytes, you will have to write out the
  2495. * offset to the same attribute, it will move the pointer.
  2496. */
  2497. static ssize_t
  2498. _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
  2499. char *buf)
  2500. {
  2501. struct Scsi_Host *shost = class_to_shost(cdev);
  2502. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2503. void *request_data;
  2504. u32 size;
  2505. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2506. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2507. "registered\n", ioc->name, __func__);
  2508. return 0;
  2509. }
  2510. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2511. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2512. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2513. "registered\n", ioc->name, __func__);
  2514. return 0;
  2515. }
  2516. if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
  2517. return 0;
  2518. size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
  2519. size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
  2520. request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
  2521. memcpy(buf, request_data, size);
  2522. return size;
  2523. }
  2524. static ssize_t
  2525. _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
  2526. const char *buf, size_t count)
  2527. {
  2528. struct Scsi_Host *shost = class_to_shost(cdev);
  2529. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2530. int val = 0;
  2531. if (sscanf(buf, "%d", &val) != 1)
  2532. return -EINVAL;
  2533. ioc->ring_buffer_offset = val;
  2534. return strlen(buf);
  2535. }
  2536. static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
  2537. _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
  2538. /*****************************************/
  2539. /**
  2540. * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
  2541. * @cdev - pointer to embedded class device
  2542. * @buf - the buffer returned
  2543. *
  2544. * A sysfs 'read/write' shost attribute.
  2545. *
  2546. * This is a mechnism to post/release host_trace_buffers
  2547. */
  2548. static ssize_t
  2549. _ctl_host_trace_buffer_enable_show(struct device *cdev,
  2550. struct device_attribute *attr, char *buf)
  2551. {
  2552. struct Scsi_Host *shost = class_to_shost(cdev);
  2553. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2554. if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
  2555. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2556. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
  2557. return snprintf(buf, PAGE_SIZE, "off\n");
  2558. else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2559. MPT2_DIAG_BUFFER_IS_RELEASED))
  2560. return snprintf(buf, PAGE_SIZE, "release\n");
  2561. else
  2562. return snprintf(buf, PAGE_SIZE, "post\n");
  2563. }
  2564. static ssize_t
  2565. _ctl_host_trace_buffer_enable_store(struct device *cdev,
  2566. struct device_attribute *attr, const char *buf, size_t count)
  2567. {
  2568. struct Scsi_Host *shost = class_to_shost(cdev);
  2569. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2570. char str[10] = "";
  2571. struct mpt2_diag_register diag_register;
  2572. u8 issue_reset = 0;
  2573. if (sscanf(buf, "%s", str) != 1)
  2574. return -EINVAL;
  2575. if (!strcmp(str, "post")) {
  2576. /* exit out if host buffers are already posted */
  2577. if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
  2578. (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2579. MPT2_DIAG_BUFFER_IS_REGISTERED) &&
  2580. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2581. MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
  2582. goto out;
  2583. memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
  2584. printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
  2585. ioc->name);
  2586. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  2587. diag_register.requested_buffer_size = (1024 * 1024);
  2588. diag_register.unique_id = 0x7075900;
  2589. ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
  2590. _ctl_diag_register_2(ioc, &diag_register);
  2591. } else if (!strcmp(str, "release")) {
  2592. /* exit out if host buffers are already released */
  2593. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
  2594. goto out;
  2595. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2596. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
  2597. goto out;
  2598. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2599. MPT2_DIAG_BUFFER_IS_RELEASED))
  2600. goto out;
  2601. printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
  2602. ioc->name);
  2603. _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
  2604. }
  2605. out:
  2606. return strlen(buf);
  2607. }
  2608. static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
  2609. _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
  2610. struct device_attribute *mpt2sas_host_attrs[] = {
  2611. &dev_attr_version_fw,
  2612. &dev_attr_version_bios,
  2613. &dev_attr_version_mpi,
  2614. &dev_attr_version_product,
  2615. &dev_attr_version_nvdata_persistent,
  2616. &dev_attr_version_nvdata_default,
  2617. &dev_attr_board_name,
  2618. &dev_attr_board_assembly,
  2619. &dev_attr_board_tracer,
  2620. &dev_attr_io_delay,
  2621. &dev_attr_device_delay,
  2622. &dev_attr_logging_level,
  2623. &dev_attr_fwfault_debug,
  2624. &dev_attr_fw_queue_depth,
  2625. &dev_attr_host_sas_address,
  2626. &dev_attr_ioc_reset_count,
  2627. &dev_attr_host_trace_buffer_size,
  2628. &dev_attr_host_trace_buffer,
  2629. &dev_attr_host_trace_buffer_enable,
  2630. &dev_attr_reply_queue_count,
  2631. NULL,
  2632. };
  2633. /**
  2634. * _ctl_device_sas_address_show - sas address
  2635. * @cdev - pointer to embedded class device
  2636. * @buf - the buffer returned
  2637. *
  2638. * This is the sas address for the target
  2639. *
  2640. * A sysfs 'read-only' shost attribute.
  2641. */
  2642. static ssize_t
  2643. _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
  2644. char *buf)
  2645. {
  2646. struct scsi_device *sdev = to_scsi_device(dev);
  2647. struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  2648. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  2649. (unsigned long long)sas_device_priv_data->sas_target->sas_address);
  2650. }
  2651. static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
  2652. /**
  2653. * _ctl_device_handle_show - device handle
  2654. * @cdev - pointer to embedded class device
  2655. * @buf - the buffer returned
  2656. *
  2657. * This is the firmware assigned device handle
  2658. *
  2659. * A sysfs 'read-only' shost attribute.
  2660. */
  2661. static ssize_t
  2662. _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
  2663. char *buf)
  2664. {
  2665. struct scsi_device *sdev = to_scsi_device(dev);
  2666. struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  2667. return snprintf(buf, PAGE_SIZE, "0x%04x\n",
  2668. sas_device_priv_data->sas_target->handle);
  2669. }
  2670. static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
  2671. struct device_attribute *mpt2sas_dev_attrs[] = {
  2672. &dev_attr_sas_address,
  2673. &dev_attr_sas_device_handle,
  2674. NULL,
  2675. };
  2676. static const struct file_operations ctl_fops = {
  2677. .owner = THIS_MODULE,
  2678. .unlocked_ioctl = _ctl_ioctl,
  2679. .release = _ctl_release,
  2680. .poll = _ctl_poll,
  2681. .fasync = _ctl_fasync,
  2682. #ifdef CONFIG_COMPAT
  2683. .compat_ioctl = _ctl_ioctl_compat,
  2684. #endif
  2685. .llseek = noop_llseek,
  2686. };
  2687. static struct miscdevice ctl_dev = {
  2688. .minor = MPT2SAS_MINOR,
  2689. .name = MPT2SAS_DEV_NAME,
  2690. .fops = &ctl_fops,
  2691. };
  2692. /**
  2693. * mpt2sas_ctl_init - main entry point for ctl.
  2694. *
  2695. */
  2696. void
  2697. mpt2sas_ctl_init(void)
  2698. {
  2699. async_queue = NULL;
  2700. if (misc_register(&ctl_dev) < 0)
  2701. printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
  2702. MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
  2703. init_waitqueue_head(&ctl_poll_wait);
  2704. }
  2705. /**
  2706. * mpt2sas_ctl_exit - exit point for ctl
  2707. *
  2708. */
  2709. void
  2710. mpt2sas_ctl_exit(void)
  2711. {
  2712. struct MPT2SAS_ADAPTER *ioc;
  2713. int i;
  2714. list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
  2715. /* free memory associated to diag buffers */
  2716. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  2717. if (!ioc->diag_buffer[i])
  2718. continue;
  2719. pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
  2720. ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
  2721. ioc->diag_buffer[i] = NULL;
  2722. ioc->diag_buffer_status[i] = 0;
  2723. }
  2724. kfree(ioc->event_log);
  2725. }
  2726. misc_deregister(&ctl_dev);
  2727. }