mpt2sas_ctl.c 72 KB

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