mpt2sas_ctl.c 85 KB

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