mpt2sas_ctl.c 76 KB

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