mpt2sas_ctl.c 70 KB

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