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