mpt2sas_ctl.c 72 KB

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