esas2r_ioctl.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. /*
  2. * linux/drivers/scsi/esas2r/esas2r_ioctl.c
  3. * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
  4. *
  5. * Copyright (c) 2001-2013 ATTO Technology, Inc.
  6. * (mailto:linuxdrivers@attotech.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. *
  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. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include "esas2r.h"
  44. /*
  45. * Buffered ioctl handlers. A buffered ioctl is one which requires that we
  46. * allocate a DMA-able memory area to communicate with the firmware. In
  47. * order to prevent continually allocating and freeing consistent memory,
  48. * we will allocate a global buffer the first time we need it and re-use
  49. * it for subsequent ioctl calls that require it.
  50. */
  51. u8 *esas2r_buffered_ioctl;
  52. dma_addr_t esas2r_buffered_ioctl_addr;
  53. u32 esas2r_buffered_ioctl_size;
  54. struct pci_dev *esas2r_buffered_ioctl_pcid;
  55. static DEFINE_SEMAPHORE(buffered_ioctl_semaphore);
  56. typedef int (*BUFFERED_IOCTL_CALLBACK)(struct esas2r_adapter *,
  57. struct esas2r_request *,
  58. struct esas2r_sg_context *,
  59. void *);
  60. typedef void (*BUFFERED_IOCTL_DONE_CALLBACK)(struct esas2r_adapter *,
  61. struct esas2r_request *, void *);
  62. struct esas2r_buffered_ioctl {
  63. struct esas2r_adapter *a;
  64. void *ioctl;
  65. u32 length;
  66. u32 control_code;
  67. u32 offset;
  68. BUFFERED_IOCTL_CALLBACK
  69. callback;
  70. void *context;
  71. BUFFERED_IOCTL_DONE_CALLBACK
  72. done_callback;
  73. void *done_context;
  74. };
  75. static void complete_fm_api_req(struct esas2r_adapter *a,
  76. struct esas2r_request *rq)
  77. {
  78. a->fm_api_command_done = 1;
  79. wake_up_interruptible(&a->fm_api_waiter);
  80. }
  81. /* Callbacks for building scatter/gather lists for FM API requests */
  82. static u32 get_physaddr_fm_api(struct esas2r_sg_context *sgc, u64 *addr)
  83. {
  84. struct esas2r_adapter *a = (struct esas2r_adapter *)sgc->adapter;
  85. int offset = sgc->cur_offset - a->save_offset;
  86. (*addr) = a->firmware.phys + offset;
  87. return a->firmware.orig_len - offset;
  88. }
  89. static u32 get_physaddr_fm_api_header(struct esas2r_sg_context *sgc, u64 *addr)
  90. {
  91. struct esas2r_adapter *a = (struct esas2r_adapter *)sgc->adapter;
  92. int offset = sgc->cur_offset - a->save_offset;
  93. (*addr) = a->firmware.header_buff_phys + offset;
  94. return sizeof(struct esas2r_flash_img) - offset;
  95. }
  96. /* Handle EXPRESS_IOCTL_RW_FIRMWARE ioctl with img_type = FW_IMG_FM_API. */
  97. static void do_fm_api(struct esas2r_adapter *a, struct esas2r_flash_img *fi)
  98. {
  99. struct esas2r_request *rq;
  100. if (mutex_lock_interruptible(&a->fm_api_mutex)) {
  101. fi->status = FI_STAT_BUSY;
  102. return;
  103. }
  104. rq = esas2r_alloc_request(a);
  105. if (rq == NULL) {
  106. fi->status = FI_STAT_BUSY;
  107. goto free_sem;
  108. }
  109. if (fi == &a->firmware.header) {
  110. a->firmware.header_buff = dma_alloc_coherent(&a->pcid->dev,
  111. (size_t)sizeof(
  112. struct
  113. esas2r_flash_img),
  114. (dma_addr_t *)&a->
  115. firmware.
  116. header_buff_phys,
  117. GFP_KERNEL);
  118. if (a->firmware.header_buff == NULL) {
  119. esas2r_debug("failed to allocate header buffer!");
  120. fi->status = FI_STAT_BUSY;
  121. goto free_req;
  122. }
  123. memcpy(a->firmware.header_buff, fi,
  124. sizeof(struct esas2r_flash_img));
  125. a->save_offset = a->firmware.header_buff;
  126. a->fm_api_sgc.get_phys_addr =
  127. (PGETPHYSADDR)get_physaddr_fm_api_header;
  128. } else {
  129. a->save_offset = (u8 *)fi;
  130. a->fm_api_sgc.get_phys_addr =
  131. (PGETPHYSADDR)get_physaddr_fm_api;
  132. }
  133. rq->comp_cb = complete_fm_api_req;
  134. a->fm_api_command_done = 0;
  135. a->fm_api_sgc.cur_offset = a->save_offset;
  136. if (!esas2r_fm_api(a, (struct esas2r_flash_img *)a->save_offset, rq,
  137. &a->fm_api_sgc))
  138. goto all_done;
  139. /* Now wait around for it to complete. */
  140. while (!a->fm_api_command_done)
  141. wait_event_interruptible(a->fm_api_waiter,
  142. a->fm_api_command_done);
  143. all_done:
  144. if (fi == &a->firmware.header) {
  145. memcpy(fi, a->firmware.header_buff,
  146. sizeof(struct esas2r_flash_img));
  147. dma_free_coherent(&a->pcid->dev,
  148. (size_t)sizeof(struct esas2r_flash_img),
  149. a->firmware.header_buff,
  150. (dma_addr_t)a->firmware.header_buff_phys);
  151. }
  152. free_req:
  153. esas2r_free_request(a, (struct esas2r_request *)rq);
  154. free_sem:
  155. mutex_unlock(&a->fm_api_mutex);
  156. return;
  157. }
  158. static void complete_nvr_req(struct esas2r_adapter *a,
  159. struct esas2r_request *rq)
  160. {
  161. a->nvram_command_done = 1;
  162. wake_up_interruptible(&a->nvram_waiter);
  163. }
  164. /* Callback for building scatter/gather lists for buffered ioctls */
  165. static u32 get_physaddr_buffered_ioctl(struct esas2r_sg_context *sgc,
  166. u64 *addr)
  167. {
  168. int offset = (u8 *)sgc->cur_offset - esas2r_buffered_ioctl;
  169. (*addr) = esas2r_buffered_ioctl_addr + offset;
  170. return esas2r_buffered_ioctl_size - offset;
  171. }
  172. static void complete_buffered_ioctl_req(struct esas2r_adapter *a,
  173. struct esas2r_request *rq)
  174. {
  175. a->buffered_ioctl_done = 1;
  176. wake_up_interruptible(&a->buffered_ioctl_waiter);
  177. }
  178. static u8 handle_buffered_ioctl(struct esas2r_buffered_ioctl *bi)
  179. {
  180. struct esas2r_adapter *a = bi->a;
  181. struct esas2r_request *rq;
  182. struct esas2r_sg_context sgc;
  183. u8 result = IOCTL_SUCCESS;
  184. if (down_interruptible(&buffered_ioctl_semaphore))
  185. return IOCTL_OUT_OF_RESOURCES;
  186. /* allocate a buffer or use the existing buffer. */
  187. if (esas2r_buffered_ioctl) {
  188. if (esas2r_buffered_ioctl_size < bi->length) {
  189. /* free the too-small buffer and get a new one */
  190. dma_free_coherent(&a->pcid->dev,
  191. (size_t)esas2r_buffered_ioctl_size,
  192. esas2r_buffered_ioctl,
  193. esas2r_buffered_ioctl_addr);
  194. goto allocate_buffer;
  195. }
  196. } else {
  197. allocate_buffer:
  198. esas2r_buffered_ioctl_size = bi->length;
  199. esas2r_buffered_ioctl_pcid = a->pcid;
  200. esas2r_buffered_ioctl = dma_alloc_coherent(&a->pcid->dev,
  201. (size_t)
  202. esas2r_buffered_ioctl_size,
  203. &
  204. esas2r_buffered_ioctl_addr,
  205. GFP_KERNEL);
  206. }
  207. if (!esas2r_buffered_ioctl) {
  208. esas2r_log(ESAS2R_LOG_CRIT,
  209. "could not allocate %d bytes of consistent memory "
  210. "for a buffered ioctl!",
  211. bi->length);
  212. esas2r_debug("buffered ioctl alloc failure");
  213. result = IOCTL_OUT_OF_RESOURCES;
  214. goto exit_cleanly;
  215. }
  216. memcpy(esas2r_buffered_ioctl, bi->ioctl, bi->length);
  217. rq = esas2r_alloc_request(a);
  218. if (rq == NULL) {
  219. esas2r_log(ESAS2R_LOG_CRIT,
  220. "could not allocate an internal request");
  221. result = IOCTL_OUT_OF_RESOURCES;
  222. esas2r_debug("buffered ioctl - no requests");
  223. goto exit_cleanly;
  224. }
  225. a->buffered_ioctl_done = 0;
  226. rq->comp_cb = complete_buffered_ioctl_req;
  227. sgc.cur_offset = esas2r_buffered_ioctl + bi->offset;
  228. sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_buffered_ioctl;
  229. sgc.length = esas2r_buffered_ioctl_size;
  230. if (!(*bi->callback)(a, rq, &sgc, bi->context)) {
  231. /* completed immediately, no need to wait */
  232. a->buffered_ioctl_done = 0;
  233. goto free_andexit_cleanly;
  234. }
  235. /* now wait around for it to complete. */
  236. while (!a->buffered_ioctl_done)
  237. wait_event_interruptible(a->buffered_ioctl_waiter,
  238. a->buffered_ioctl_done);
  239. free_andexit_cleanly:
  240. if (result == IOCTL_SUCCESS && bi->done_callback)
  241. (*bi->done_callback)(a, rq, bi->done_context);
  242. esas2r_free_request(a, rq);
  243. exit_cleanly:
  244. if (result == IOCTL_SUCCESS)
  245. memcpy(bi->ioctl, esas2r_buffered_ioctl, bi->length);
  246. up(&buffered_ioctl_semaphore);
  247. return result;
  248. }
  249. /* SMP ioctl support */
  250. static int smp_ioctl_callback(struct esas2r_adapter *a,
  251. struct esas2r_request *rq,
  252. struct esas2r_sg_context *sgc, void *context)
  253. {
  254. struct atto_ioctl_smp *si =
  255. (struct atto_ioctl_smp *)esas2r_buffered_ioctl;
  256. esas2r_sgc_init(sgc, a, rq, rq->vrq->ioctl.sge);
  257. esas2r_build_ioctl_req(a, rq, sgc->length, VDA_IOCTL_SMP);
  258. if (!esas2r_build_sg_list(a, rq, sgc)) {
  259. si->status = ATTO_STS_OUT_OF_RSRC;
  260. return false;
  261. }
  262. esas2r_start_request(a, rq);
  263. return true;
  264. }
  265. static u8 handle_smp_ioctl(struct esas2r_adapter *a, struct atto_ioctl_smp *si)
  266. {
  267. struct esas2r_buffered_ioctl bi;
  268. memset(&bi, 0, sizeof(bi));
  269. bi.a = a;
  270. bi.ioctl = si;
  271. bi.length = sizeof(struct atto_ioctl_smp)
  272. + le32_to_cpu(si->req_length)
  273. + le32_to_cpu(si->rsp_length);
  274. bi.offset = 0;
  275. bi.callback = smp_ioctl_callback;
  276. return handle_buffered_ioctl(&bi);
  277. }
  278. /* CSMI ioctl support */
  279. static void esas2r_csmi_ioctl_tunnel_comp_cb(struct esas2r_adapter *a,
  280. struct esas2r_request *rq)
  281. {
  282. rq->target_id = le16_to_cpu(rq->func_rsp.ioctl_rsp.csmi.target_id);
  283. rq->vrq->scsi.flags |= cpu_to_le32(rq->func_rsp.ioctl_rsp.csmi.lun);
  284. /* Now call the original completion callback. */
  285. (*rq->aux_req_cb)(a, rq);
  286. }
  287. /* Tunnel a CSMI IOCTL to the back end driver for processing. */
  288. static bool csmi_ioctl_tunnel(struct esas2r_adapter *a,
  289. union atto_ioctl_csmi *ci,
  290. struct esas2r_request *rq,
  291. struct esas2r_sg_context *sgc,
  292. u32 ctrl_code,
  293. u16 target_id)
  294. {
  295. struct atto_vda_ioctl_req *ioctl = &rq->vrq->ioctl;
  296. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  297. return false;
  298. esas2r_sgc_init(sgc, a, rq, rq->vrq->ioctl.sge);
  299. esas2r_build_ioctl_req(a, rq, sgc->length, VDA_IOCTL_CSMI);
  300. ioctl->csmi.ctrl_code = cpu_to_le32(ctrl_code);
  301. ioctl->csmi.target_id = cpu_to_le16(target_id);
  302. ioctl->csmi.lun = (u8)le32_to_cpu(rq->vrq->scsi.flags);
  303. /*
  304. * Always usurp the completion callback since the interrupt callback
  305. * mechanism may be used.
  306. */
  307. rq->aux_req_cx = ci;
  308. rq->aux_req_cb = rq->comp_cb;
  309. rq->comp_cb = esas2r_csmi_ioctl_tunnel_comp_cb;
  310. if (!esas2r_build_sg_list(a, rq, sgc))
  311. return false;
  312. esas2r_start_request(a, rq);
  313. return true;
  314. }
  315. static bool check_lun(struct scsi_lun lun)
  316. {
  317. bool result;
  318. result = ((lun.scsi_lun[7] == 0) &&
  319. (lun.scsi_lun[6] == 0) &&
  320. (lun.scsi_lun[5] == 0) &&
  321. (lun.scsi_lun[4] == 0) &&
  322. (lun.scsi_lun[3] == 0) &&
  323. (lun.scsi_lun[2] == 0) &&
  324. /* Byte 1 is intentionally skipped */
  325. (lun.scsi_lun[0] == 0));
  326. return result;
  327. }
  328. static int csmi_ioctl_callback(struct esas2r_adapter *a,
  329. struct esas2r_request *rq,
  330. struct esas2r_sg_context *sgc, void *context)
  331. {
  332. struct atto_csmi *ci = (struct atto_csmi *)context;
  333. union atto_ioctl_csmi *ioctl_csmi =
  334. (union atto_ioctl_csmi *)esas2r_buffered_ioctl;
  335. u8 path = 0;
  336. u8 tid = 0;
  337. u8 lun = 0;
  338. u32 sts = CSMI_STS_SUCCESS;
  339. struct esas2r_target *t;
  340. unsigned long flags;
  341. if (ci->control_code == CSMI_CC_GET_DEV_ADDR) {
  342. struct atto_csmi_get_dev_addr *gda = &ci->data.dev_addr;
  343. path = gda->path_id;
  344. tid = gda->target_id;
  345. lun = gda->lun;
  346. } else if (ci->control_code == CSMI_CC_TASK_MGT) {
  347. struct atto_csmi_task_mgmt *tm = &ci->data.tsk_mgt;
  348. path = tm->path_id;
  349. tid = tm->target_id;
  350. lun = tm->lun;
  351. }
  352. if (path > 0) {
  353. rq->func_rsp.ioctl_rsp.csmi.csmi_status = cpu_to_le32(
  354. CSMI_STS_INV_PARAM);
  355. return false;
  356. }
  357. rq->target_id = tid;
  358. rq->vrq->scsi.flags |= cpu_to_le32(lun);
  359. switch (ci->control_code) {
  360. case CSMI_CC_GET_DRVR_INFO:
  361. {
  362. struct atto_csmi_get_driver_info *gdi = &ioctl_csmi->drvr_info;
  363. strcpy(gdi->description, esas2r_get_model_name(a));
  364. gdi->csmi_major_rev = CSMI_MAJOR_REV;
  365. gdi->csmi_minor_rev = CSMI_MINOR_REV;
  366. break;
  367. }
  368. case CSMI_CC_GET_CNTLR_CFG:
  369. {
  370. struct atto_csmi_get_cntlr_cfg *gcc = &ioctl_csmi->cntlr_cfg;
  371. gcc->base_io_addr = 0;
  372. pci_read_config_dword(a->pcid, PCI_BASE_ADDRESS_2,
  373. &gcc->base_memaddr_lo);
  374. pci_read_config_dword(a->pcid, PCI_BASE_ADDRESS_3,
  375. &gcc->base_memaddr_hi);
  376. gcc->board_id = MAKEDWORD(a->pcid->subsystem_device,
  377. a->pcid->subsystem_vendor);
  378. gcc->slot_num = CSMI_SLOT_NUM_UNKNOWN;
  379. gcc->cntlr_class = CSMI_CNTLR_CLASS_HBA;
  380. gcc->io_bus_type = CSMI_BUS_TYPE_PCI;
  381. gcc->pci_addr.bus_num = a->pcid->bus->number;
  382. gcc->pci_addr.device_num = PCI_SLOT(a->pcid->devfn);
  383. gcc->pci_addr.function_num = PCI_FUNC(a->pcid->devfn);
  384. memset(gcc->serial_num, 0, sizeof(gcc->serial_num));
  385. gcc->major_rev = LOBYTE(LOWORD(a->fw_version));
  386. gcc->minor_rev = HIBYTE(LOWORD(a->fw_version));
  387. gcc->build_rev = LOBYTE(HIWORD(a->fw_version));
  388. gcc->release_rev = HIBYTE(HIWORD(a->fw_version));
  389. gcc->bios_major_rev = HIBYTE(HIWORD(a->flash_ver));
  390. gcc->bios_minor_rev = LOBYTE(HIWORD(a->flash_ver));
  391. gcc->bios_build_rev = LOWORD(a->flash_ver);
  392. if (test_bit(AF2_THUNDERLINK, &a->flags2))
  393. gcc->cntlr_flags = CSMI_CNTLRF_SAS_HBA
  394. | CSMI_CNTLRF_SATA_HBA;
  395. else
  396. gcc->cntlr_flags = CSMI_CNTLRF_SAS_RAID
  397. | CSMI_CNTLRF_SATA_RAID;
  398. gcc->rrom_major_rev = 0;
  399. gcc->rrom_minor_rev = 0;
  400. gcc->rrom_build_rev = 0;
  401. gcc->rrom_release_rev = 0;
  402. gcc->rrom_biosmajor_rev = 0;
  403. gcc->rrom_biosminor_rev = 0;
  404. gcc->rrom_biosbuild_rev = 0;
  405. gcc->rrom_biosrelease_rev = 0;
  406. break;
  407. }
  408. case CSMI_CC_GET_CNTLR_STS:
  409. {
  410. struct atto_csmi_get_cntlr_sts *gcs = &ioctl_csmi->cntlr_sts;
  411. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  412. gcs->status = CSMI_CNTLR_STS_FAILED;
  413. else
  414. gcs->status = CSMI_CNTLR_STS_GOOD;
  415. gcs->offline_reason = CSMI_OFFLINE_NO_REASON;
  416. break;
  417. }
  418. case CSMI_CC_FW_DOWNLOAD:
  419. case CSMI_CC_GET_RAID_INFO:
  420. case CSMI_CC_GET_RAID_CFG:
  421. sts = CSMI_STS_BAD_CTRL_CODE;
  422. break;
  423. case CSMI_CC_SMP_PASSTHRU:
  424. case CSMI_CC_SSP_PASSTHRU:
  425. case CSMI_CC_STP_PASSTHRU:
  426. case CSMI_CC_GET_PHY_INFO:
  427. case CSMI_CC_SET_PHY_INFO:
  428. case CSMI_CC_GET_LINK_ERRORS:
  429. case CSMI_CC_GET_SATA_SIG:
  430. case CSMI_CC_GET_CONN_INFO:
  431. case CSMI_CC_PHY_CTRL:
  432. if (!csmi_ioctl_tunnel(a, ioctl_csmi, rq, sgc,
  433. ci->control_code,
  434. ESAS2R_TARG_ID_INV)) {
  435. sts = CSMI_STS_FAILED;
  436. break;
  437. }
  438. return true;
  439. case CSMI_CC_GET_SCSI_ADDR:
  440. {
  441. struct atto_csmi_get_scsi_addr *gsa = &ioctl_csmi->scsi_addr;
  442. struct scsi_lun lun;
  443. memcpy(&lun, gsa->sas_lun, sizeof(struct scsi_lun));
  444. if (!check_lun(lun)) {
  445. sts = CSMI_STS_NO_SCSI_ADDR;
  446. break;
  447. }
  448. /* make sure the device is present */
  449. spin_lock_irqsave(&a->mem_lock, flags);
  450. t = esas2r_targ_db_find_by_sas_addr(a, (u64 *)gsa->sas_addr);
  451. spin_unlock_irqrestore(&a->mem_lock, flags);
  452. if (t == NULL) {
  453. sts = CSMI_STS_NO_SCSI_ADDR;
  454. break;
  455. }
  456. gsa->host_index = 0xFF;
  457. gsa->lun = gsa->sas_lun[1];
  458. rq->target_id = esas2r_targ_get_id(t, a);
  459. break;
  460. }
  461. case CSMI_CC_GET_DEV_ADDR:
  462. {
  463. struct atto_csmi_get_dev_addr *gda = &ioctl_csmi->dev_addr;
  464. /* make sure the target is present */
  465. t = a->targetdb + rq->target_id;
  466. if (t >= a->targetdb_end
  467. || t->target_state != TS_PRESENT
  468. || t->sas_addr == 0) {
  469. sts = CSMI_STS_NO_DEV_ADDR;
  470. break;
  471. }
  472. /* fill in the result */
  473. *(u64 *)gda->sas_addr = t->sas_addr;
  474. memset(gda->sas_lun, 0, sizeof(gda->sas_lun));
  475. gda->sas_lun[1] = (u8)le32_to_cpu(rq->vrq->scsi.flags);
  476. break;
  477. }
  478. case CSMI_CC_TASK_MGT:
  479. /* make sure the target is present */
  480. t = a->targetdb + rq->target_id;
  481. if (t >= a->targetdb_end
  482. || t->target_state != TS_PRESENT
  483. || !(t->flags & TF_PASS_THRU)) {
  484. sts = CSMI_STS_NO_DEV_ADDR;
  485. break;
  486. }
  487. if (!csmi_ioctl_tunnel(a, ioctl_csmi, rq, sgc,
  488. ci->control_code,
  489. t->phys_targ_id)) {
  490. sts = CSMI_STS_FAILED;
  491. break;
  492. }
  493. return true;
  494. default:
  495. sts = CSMI_STS_BAD_CTRL_CODE;
  496. break;
  497. }
  498. rq->func_rsp.ioctl_rsp.csmi.csmi_status = cpu_to_le32(sts);
  499. return false;
  500. }
  501. static void csmi_ioctl_done_callback(struct esas2r_adapter *a,
  502. struct esas2r_request *rq, void *context)
  503. {
  504. struct atto_csmi *ci = (struct atto_csmi *)context;
  505. union atto_ioctl_csmi *ioctl_csmi =
  506. (union atto_ioctl_csmi *)esas2r_buffered_ioctl;
  507. switch (ci->control_code) {
  508. case CSMI_CC_GET_DRVR_INFO:
  509. {
  510. struct atto_csmi_get_driver_info *gdi =
  511. &ioctl_csmi->drvr_info;
  512. strcpy(gdi->name, ESAS2R_VERSION_STR);
  513. gdi->major_rev = ESAS2R_MAJOR_REV;
  514. gdi->minor_rev = ESAS2R_MINOR_REV;
  515. gdi->build_rev = 0;
  516. gdi->release_rev = 0;
  517. break;
  518. }
  519. case CSMI_CC_GET_SCSI_ADDR:
  520. {
  521. struct atto_csmi_get_scsi_addr *gsa = &ioctl_csmi->scsi_addr;
  522. if (le32_to_cpu(rq->func_rsp.ioctl_rsp.csmi.csmi_status) ==
  523. CSMI_STS_SUCCESS) {
  524. gsa->target_id = rq->target_id;
  525. gsa->path_id = 0;
  526. }
  527. break;
  528. }
  529. }
  530. ci->status = le32_to_cpu(rq->func_rsp.ioctl_rsp.csmi.csmi_status);
  531. }
  532. static u8 handle_csmi_ioctl(struct esas2r_adapter *a, struct atto_csmi *ci)
  533. {
  534. struct esas2r_buffered_ioctl bi;
  535. memset(&bi, 0, sizeof(bi));
  536. bi.a = a;
  537. bi.ioctl = &ci->data;
  538. bi.length = sizeof(union atto_ioctl_csmi);
  539. bi.offset = 0;
  540. bi.callback = csmi_ioctl_callback;
  541. bi.context = ci;
  542. bi.done_callback = csmi_ioctl_done_callback;
  543. bi.done_context = ci;
  544. return handle_buffered_ioctl(&bi);
  545. }
  546. /* ATTO HBA ioctl support */
  547. /* Tunnel an ATTO HBA IOCTL to the back end driver for processing. */
  548. static bool hba_ioctl_tunnel(struct esas2r_adapter *a,
  549. struct atto_ioctl *hi,
  550. struct esas2r_request *rq,
  551. struct esas2r_sg_context *sgc)
  552. {
  553. esas2r_sgc_init(sgc, a, rq, rq->vrq->ioctl.sge);
  554. esas2r_build_ioctl_req(a, rq, sgc->length, VDA_IOCTL_HBA);
  555. if (!esas2r_build_sg_list(a, rq, sgc)) {
  556. hi->status = ATTO_STS_OUT_OF_RSRC;
  557. return false;
  558. }
  559. esas2r_start_request(a, rq);
  560. return true;
  561. }
  562. static void scsi_passthru_comp_cb(struct esas2r_adapter *a,
  563. struct esas2r_request *rq)
  564. {
  565. struct atto_ioctl *hi = (struct atto_ioctl *)rq->aux_req_cx;
  566. struct atto_hba_scsi_pass_thru *spt = &hi->data.scsi_pass_thru;
  567. u8 sts = ATTO_SPT_RS_FAILED;
  568. spt->scsi_status = rq->func_rsp.scsi_rsp.scsi_stat;
  569. spt->sense_length = rq->sense_len;
  570. spt->residual_length =
  571. le32_to_cpu(rq->func_rsp.scsi_rsp.residual_length);
  572. switch (rq->req_stat) {
  573. case RS_SUCCESS:
  574. case RS_SCSI_ERROR:
  575. sts = ATTO_SPT_RS_SUCCESS;
  576. break;
  577. case RS_UNDERRUN:
  578. sts = ATTO_SPT_RS_UNDERRUN;
  579. break;
  580. case RS_OVERRUN:
  581. sts = ATTO_SPT_RS_OVERRUN;
  582. break;
  583. case RS_SEL:
  584. case RS_SEL2:
  585. sts = ATTO_SPT_RS_NO_DEVICE;
  586. break;
  587. case RS_NO_LUN:
  588. sts = ATTO_SPT_RS_NO_LUN;
  589. break;
  590. case RS_TIMEOUT:
  591. sts = ATTO_SPT_RS_TIMEOUT;
  592. break;
  593. case RS_DEGRADED:
  594. sts = ATTO_SPT_RS_DEGRADED;
  595. break;
  596. case RS_BUSY:
  597. sts = ATTO_SPT_RS_BUSY;
  598. break;
  599. case RS_ABORTED:
  600. sts = ATTO_SPT_RS_ABORTED;
  601. break;
  602. case RS_RESET:
  603. sts = ATTO_SPT_RS_BUS_RESET;
  604. break;
  605. }
  606. spt->req_status = sts;
  607. /* Update the target ID to the next one present. */
  608. spt->target_id =
  609. esas2r_targ_db_find_next_present(a, (u16)spt->target_id);
  610. /* Done, call the completion callback. */
  611. (*rq->aux_req_cb)(a, rq);
  612. }
  613. static int hba_ioctl_callback(struct esas2r_adapter *a,
  614. struct esas2r_request *rq,
  615. struct esas2r_sg_context *sgc,
  616. void *context)
  617. {
  618. struct atto_ioctl *hi = (struct atto_ioctl *)esas2r_buffered_ioctl;
  619. hi->status = ATTO_STS_SUCCESS;
  620. switch (hi->function) {
  621. case ATTO_FUNC_GET_ADAP_INFO:
  622. {
  623. u8 *class_code = (u8 *)&a->pcid->class;
  624. struct atto_hba_get_adapter_info *gai =
  625. &hi->data.get_adap_info;
  626. int pcie_cap_reg;
  627. if (hi->flags & HBAF_TUNNEL) {
  628. hi->status = ATTO_STS_UNSUPPORTED;
  629. break;
  630. }
  631. if (hi->version > ATTO_VER_GET_ADAP_INFO0) {
  632. hi->status = ATTO_STS_INV_VERSION;
  633. hi->version = ATTO_VER_GET_ADAP_INFO0;
  634. break;
  635. }
  636. memset(gai, 0, sizeof(*gai));
  637. gai->pci.vendor_id = a->pcid->vendor;
  638. gai->pci.device_id = a->pcid->device;
  639. gai->pci.ss_vendor_id = a->pcid->subsystem_vendor;
  640. gai->pci.ss_device_id = a->pcid->subsystem_device;
  641. gai->pci.class_code[0] = class_code[0];
  642. gai->pci.class_code[1] = class_code[1];
  643. gai->pci.class_code[2] = class_code[2];
  644. gai->pci.rev_id = a->pcid->revision;
  645. gai->pci.bus_num = a->pcid->bus->number;
  646. gai->pci.dev_num = PCI_SLOT(a->pcid->devfn);
  647. gai->pci.func_num = PCI_FUNC(a->pcid->devfn);
  648. pcie_cap_reg = pci_find_capability(a->pcid, PCI_CAP_ID_EXP);
  649. if (pcie_cap_reg) {
  650. u16 stat;
  651. u32 caps;
  652. pci_read_config_word(a->pcid,
  653. pcie_cap_reg + PCI_EXP_LNKSTA,
  654. &stat);
  655. pci_read_config_dword(a->pcid,
  656. pcie_cap_reg + PCI_EXP_LNKCAP,
  657. &caps);
  658. gai->pci.link_speed_curr =
  659. (u8)(stat & PCI_EXP_LNKSTA_CLS);
  660. gai->pci.link_speed_max =
  661. (u8)(caps & PCI_EXP_LNKCAP_SLS);
  662. gai->pci.link_width_curr =
  663. (u8)((stat & PCI_EXP_LNKSTA_NLW)
  664. >> PCI_EXP_LNKSTA_NLW_SHIFT);
  665. gai->pci.link_width_max =
  666. (u8)((caps & PCI_EXP_LNKCAP_MLW)
  667. >> 4);
  668. }
  669. gai->pci.msi_vector_cnt = 1;
  670. if (a->pcid->msix_enabled)
  671. gai->pci.interrupt_mode = ATTO_GAI_PCIIM_MSIX;
  672. else if (a->pcid->msi_enabled)
  673. gai->pci.interrupt_mode = ATTO_GAI_PCIIM_MSI;
  674. else
  675. gai->pci.interrupt_mode = ATTO_GAI_PCIIM_LEGACY;
  676. gai->adap_type = ATTO_GAI_AT_ESASRAID2;
  677. if (test_bit(AF2_THUNDERLINK, &a->flags2))
  678. gai->adap_type = ATTO_GAI_AT_TLSASHBA;
  679. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  680. gai->adap_flags |= ATTO_GAI_AF_DEGRADED;
  681. gai->adap_flags |= ATTO_GAI_AF_SPT_SUPP |
  682. ATTO_GAI_AF_DEVADDR_SUPP;
  683. if (a->pcid->subsystem_device == ATTO_ESAS_R60F
  684. || a->pcid->subsystem_device == ATTO_ESAS_R608
  685. || a->pcid->subsystem_device == ATTO_ESAS_R644
  686. || a->pcid->subsystem_device == ATTO_TSSC_3808E)
  687. gai->adap_flags |= ATTO_GAI_AF_VIRT_SES;
  688. gai->num_ports = ESAS2R_NUM_PHYS;
  689. gai->num_phys = ESAS2R_NUM_PHYS;
  690. strcpy(gai->firmware_rev, a->fw_rev);
  691. strcpy(gai->flash_rev, a->flash_rev);
  692. strcpy(gai->model_name_short, esas2r_get_model_name_short(a));
  693. strcpy(gai->model_name, esas2r_get_model_name(a));
  694. gai->num_targets = ESAS2R_MAX_TARGETS;
  695. gai->num_busses = 1;
  696. gai->num_targsper_bus = gai->num_targets;
  697. gai->num_lunsper_targ = 256;
  698. if (a->pcid->subsystem_device == ATTO_ESAS_R6F0
  699. || a->pcid->subsystem_device == ATTO_ESAS_R60F)
  700. gai->num_connectors = 4;
  701. else
  702. gai->num_connectors = 2;
  703. gai->adap_flags2 |= ATTO_GAI_AF2_ADAP_CTRL_SUPP;
  704. gai->num_targets_backend = a->num_targets_backend;
  705. gai->tunnel_flags = a->ioctl_tunnel
  706. & (ATTO_GAI_TF_MEM_RW
  707. | ATTO_GAI_TF_TRACE
  708. | ATTO_GAI_TF_SCSI_PASS_THRU
  709. | ATTO_GAI_TF_GET_DEV_ADDR
  710. | ATTO_GAI_TF_PHY_CTRL
  711. | ATTO_GAI_TF_CONN_CTRL
  712. | ATTO_GAI_TF_GET_DEV_INFO);
  713. break;
  714. }
  715. case ATTO_FUNC_GET_ADAP_ADDR:
  716. {
  717. struct atto_hba_get_adapter_address *gaa =
  718. &hi->data.get_adap_addr;
  719. if (hi->flags & HBAF_TUNNEL) {
  720. hi->status = ATTO_STS_UNSUPPORTED;
  721. break;
  722. }
  723. if (hi->version > ATTO_VER_GET_ADAP_ADDR0) {
  724. hi->status = ATTO_STS_INV_VERSION;
  725. hi->version = ATTO_VER_GET_ADAP_ADDR0;
  726. } else if (gaa->addr_type == ATTO_GAA_AT_PORT
  727. || gaa->addr_type == ATTO_GAA_AT_NODE) {
  728. if (gaa->addr_type == ATTO_GAA_AT_PORT
  729. && gaa->port_id >= ESAS2R_NUM_PHYS) {
  730. hi->status = ATTO_STS_NOT_APPL;
  731. } else {
  732. memcpy((u64 *)gaa->address,
  733. &a->nvram->sas_addr[0], sizeof(u64));
  734. gaa->addr_len = sizeof(u64);
  735. }
  736. } else {
  737. hi->status = ATTO_STS_INV_PARAM;
  738. }
  739. break;
  740. }
  741. case ATTO_FUNC_MEM_RW:
  742. {
  743. if (hi->flags & HBAF_TUNNEL) {
  744. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  745. return true;
  746. break;
  747. }
  748. hi->status = ATTO_STS_UNSUPPORTED;
  749. break;
  750. }
  751. case ATTO_FUNC_TRACE:
  752. {
  753. struct atto_hba_trace *trc = &hi->data.trace;
  754. if (hi->flags & HBAF_TUNNEL) {
  755. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  756. return true;
  757. break;
  758. }
  759. if (hi->version > ATTO_VER_TRACE1) {
  760. hi->status = ATTO_STS_INV_VERSION;
  761. hi->version = ATTO_VER_TRACE1;
  762. break;
  763. }
  764. if (trc->trace_type == ATTO_TRC_TT_FWCOREDUMP
  765. && hi->version >= ATTO_VER_TRACE1) {
  766. if (trc->trace_func == ATTO_TRC_TF_UPLOAD) {
  767. u32 len = hi->data_length;
  768. u32 offset = trc->current_offset;
  769. u32 total_len = ESAS2R_FWCOREDUMP_SZ;
  770. /* Size is zero if a core dump isn't present */
  771. if (!test_bit(AF2_COREDUMP_SAVED, &a->flags2))
  772. total_len = 0;
  773. if (len > total_len)
  774. len = total_len;
  775. if (offset >= total_len
  776. || offset + len > total_len
  777. || len == 0) {
  778. hi->status = ATTO_STS_INV_PARAM;
  779. break;
  780. }
  781. memcpy(trc + 1,
  782. a->fw_coredump_buff + offset,
  783. len);
  784. hi->data_length = len;
  785. } else if (trc->trace_func == ATTO_TRC_TF_RESET) {
  786. memset(a->fw_coredump_buff, 0,
  787. ESAS2R_FWCOREDUMP_SZ);
  788. clear_bit(AF2_COREDUMP_SAVED, &a->flags2);
  789. } else if (trc->trace_func != ATTO_TRC_TF_GET_INFO) {
  790. hi->status = ATTO_STS_UNSUPPORTED;
  791. break;
  792. }
  793. /* Always return all the info we can. */
  794. trc->trace_mask = 0;
  795. trc->current_offset = 0;
  796. trc->total_length = ESAS2R_FWCOREDUMP_SZ;
  797. /* Return zero length buffer if core dump not present */
  798. if (!test_bit(AF2_COREDUMP_SAVED, &a->flags2))
  799. trc->total_length = 0;
  800. } else {
  801. hi->status = ATTO_STS_UNSUPPORTED;
  802. }
  803. break;
  804. }
  805. case ATTO_FUNC_SCSI_PASS_THRU:
  806. {
  807. struct atto_hba_scsi_pass_thru *spt = &hi->data.scsi_pass_thru;
  808. struct scsi_lun lun;
  809. memcpy(&lun, spt->lun, sizeof(struct scsi_lun));
  810. if (hi->flags & HBAF_TUNNEL) {
  811. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  812. return true;
  813. break;
  814. }
  815. if (hi->version > ATTO_VER_SCSI_PASS_THRU0) {
  816. hi->status = ATTO_STS_INV_VERSION;
  817. hi->version = ATTO_VER_SCSI_PASS_THRU0;
  818. break;
  819. }
  820. if (spt->target_id >= ESAS2R_MAX_TARGETS || !check_lun(lun)) {
  821. hi->status = ATTO_STS_INV_PARAM;
  822. break;
  823. }
  824. esas2r_sgc_init(sgc, a, rq, NULL);
  825. sgc->length = hi->data_length;
  826. sgc->cur_offset += offsetof(struct atto_ioctl, data.byte)
  827. + sizeof(struct atto_hba_scsi_pass_thru);
  828. /* Finish request initialization */
  829. rq->target_id = (u16)spt->target_id;
  830. rq->vrq->scsi.flags |= cpu_to_le32(spt->lun[1]);
  831. memcpy(rq->vrq->scsi.cdb, spt->cdb, 16);
  832. rq->vrq->scsi.length = cpu_to_le32(hi->data_length);
  833. rq->sense_len = spt->sense_length;
  834. rq->sense_buf = (u8 *)spt->sense_data;
  835. /* NOTE: we ignore spt->timeout */
  836. /*
  837. * always usurp the completion callback since the interrupt
  838. * callback mechanism may be used.
  839. */
  840. rq->aux_req_cx = hi;
  841. rq->aux_req_cb = rq->comp_cb;
  842. rq->comp_cb = scsi_passthru_comp_cb;
  843. if (spt->flags & ATTO_SPTF_DATA_IN) {
  844. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
  845. } else if (spt->flags & ATTO_SPTF_DATA_OUT) {
  846. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
  847. } else {
  848. if (sgc->length) {
  849. hi->status = ATTO_STS_INV_PARAM;
  850. break;
  851. }
  852. }
  853. if (spt->flags & ATTO_SPTF_ORDERED_Q)
  854. rq->vrq->scsi.flags |=
  855. cpu_to_le32(FCP_CMND_TA_ORDRD_Q);
  856. else if (spt->flags & ATTO_SPTF_HEAD_OF_Q)
  857. rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_TA_HEAD_Q);
  858. if (!esas2r_build_sg_list(a, rq, sgc)) {
  859. hi->status = ATTO_STS_OUT_OF_RSRC;
  860. break;
  861. }
  862. esas2r_start_request(a, rq);
  863. return true;
  864. }
  865. case ATTO_FUNC_GET_DEV_ADDR:
  866. {
  867. struct atto_hba_get_device_address *gda =
  868. &hi->data.get_dev_addr;
  869. struct esas2r_target *t;
  870. if (hi->flags & HBAF_TUNNEL) {
  871. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  872. return true;
  873. break;
  874. }
  875. if (hi->version > ATTO_VER_GET_DEV_ADDR0) {
  876. hi->status = ATTO_STS_INV_VERSION;
  877. hi->version = ATTO_VER_GET_DEV_ADDR0;
  878. break;
  879. }
  880. if (gda->target_id >= ESAS2R_MAX_TARGETS) {
  881. hi->status = ATTO_STS_INV_PARAM;
  882. break;
  883. }
  884. t = a->targetdb + (u16)gda->target_id;
  885. if (t->target_state != TS_PRESENT) {
  886. hi->status = ATTO_STS_FAILED;
  887. } else if (gda->addr_type == ATTO_GDA_AT_PORT) {
  888. if (t->sas_addr == 0) {
  889. hi->status = ATTO_STS_UNSUPPORTED;
  890. } else {
  891. *(u64 *)gda->address = t->sas_addr;
  892. gda->addr_len = sizeof(u64);
  893. }
  894. } else if (gda->addr_type == ATTO_GDA_AT_NODE) {
  895. hi->status = ATTO_STS_NOT_APPL;
  896. } else {
  897. hi->status = ATTO_STS_INV_PARAM;
  898. }
  899. /* update the target ID to the next one present. */
  900. gda->target_id =
  901. esas2r_targ_db_find_next_present(a,
  902. (u16)gda->target_id);
  903. break;
  904. }
  905. case ATTO_FUNC_PHY_CTRL:
  906. case ATTO_FUNC_CONN_CTRL:
  907. {
  908. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  909. return true;
  910. break;
  911. }
  912. case ATTO_FUNC_ADAP_CTRL:
  913. {
  914. struct atto_hba_adap_ctrl *ac = &hi->data.adap_ctrl;
  915. if (hi->flags & HBAF_TUNNEL) {
  916. hi->status = ATTO_STS_UNSUPPORTED;
  917. break;
  918. }
  919. if (hi->version > ATTO_VER_ADAP_CTRL0) {
  920. hi->status = ATTO_STS_INV_VERSION;
  921. hi->version = ATTO_VER_ADAP_CTRL0;
  922. break;
  923. }
  924. if (ac->adap_func == ATTO_AC_AF_HARD_RST) {
  925. esas2r_reset_adapter(a);
  926. } else if (ac->adap_func != ATTO_AC_AF_GET_STATE) {
  927. hi->status = ATTO_STS_UNSUPPORTED;
  928. break;
  929. }
  930. if (test_bit(AF_CHPRST_NEEDED, &a->flags))
  931. ac->adap_state = ATTO_AC_AS_RST_SCHED;
  932. else if (test_bit(AF_CHPRST_PENDING, &a->flags))
  933. ac->adap_state = ATTO_AC_AS_RST_IN_PROG;
  934. else if (test_bit(AF_DISC_PENDING, &a->flags))
  935. ac->adap_state = ATTO_AC_AS_RST_DISC;
  936. else if (test_bit(AF_DISABLED, &a->flags))
  937. ac->adap_state = ATTO_AC_AS_DISABLED;
  938. else if (test_bit(AF_DEGRADED_MODE, &a->flags))
  939. ac->adap_state = ATTO_AC_AS_DEGRADED;
  940. else
  941. ac->adap_state = ATTO_AC_AS_OK;
  942. break;
  943. }
  944. case ATTO_FUNC_GET_DEV_INFO:
  945. {
  946. struct atto_hba_get_device_info *gdi = &hi->data.get_dev_info;
  947. struct esas2r_target *t;
  948. if (hi->flags & HBAF_TUNNEL) {
  949. if (hba_ioctl_tunnel(a, hi, rq, sgc))
  950. return true;
  951. break;
  952. }
  953. if (hi->version > ATTO_VER_GET_DEV_INFO0) {
  954. hi->status = ATTO_STS_INV_VERSION;
  955. hi->version = ATTO_VER_GET_DEV_INFO0;
  956. break;
  957. }
  958. if (gdi->target_id >= ESAS2R_MAX_TARGETS) {
  959. hi->status = ATTO_STS_INV_PARAM;
  960. break;
  961. }
  962. t = a->targetdb + (u16)gdi->target_id;
  963. /* update the target ID to the next one present. */
  964. gdi->target_id =
  965. esas2r_targ_db_find_next_present(a,
  966. (u16)gdi->target_id);
  967. if (t->target_state != TS_PRESENT) {
  968. hi->status = ATTO_STS_FAILED;
  969. break;
  970. }
  971. hi->status = ATTO_STS_UNSUPPORTED;
  972. break;
  973. }
  974. default:
  975. hi->status = ATTO_STS_INV_FUNC;
  976. break;
  977. }
  978. return false;
  979. }
  980. static void hba_ioctl_done_callback(struct esas2r_adapter *a,
  981. struct esas2r_request *rq, void *context)
  982. {
  983. struct atto_ioctl *ioctl_hba =
  984. (struct atto_ioctl *)esas2r_buffered_ioctl;
  985. esas2r_debug("hba_ioctl_done_callback %d", a->index);
  986. if (ioctl_hba->function == ATTO_FUNC_GET_ADAP_INFO) {
  987. struct atto_hba_get_adapter_info *gai =
  988. &ioctl_hba->data.get_adap_info;
  989. esas2r_debug("ATTO_FUNC_GET_ADAP_INFO");
  990. gai->drvr_rev_major = ESAS2R_MAJOR_REV;
  991. gai->drvr_rev_minor = ESAS2R_MINOR_REV;
  992. strcpy(gai->drvr_rev_ascii, ESAS2R_VERSION_STR);
  993. strcpy(gai->drvr_name, ESAS2R_DRVR_NAME);
  994. gai->num_busses = 1;
  995. gai->num_targsper_bus = ESAS2R_MAX_ID + 1;
  996. gai->num_lunsper_targ = 1;
  997. }
  998. }
  999. u8 handle_hba_ioctl(struct esas2r_adapter *a,
  1000. struct atto_ioctl *ioctl_hba)
  1001. {
  1002. struct esas2r_buffered_ioctl bi;
  1003. memset(&bi, 0, sizeof(bi));
  1004. bi.a = a;
  1005. bi.ioctl = ioctl_hba;
  1006. bi.length = sizeof(struct atto_ioctl) + ioctl_hba->data_length;
  1007. bi.callback = hba_ioctl_callback;
  1008. bi.context = NULL;
  1009. bi.done_callback = hba_ioctl_done_callback;
  1010. bi.done_context = NULL;
  1011. bi.offset = 0;
  1012. return handle_buffered_ioctl(&bi);
  1013. }
  1014. int esas2r_write_params(struct esas2r_adapter *a, struct esas2r_request *rq,
  1015. struct esas2r_sas_nvram *data)
  1016. {
  1017. int result = 0;
  1018. a->nvram_command_done = 0;
  1019. rq->comp_cb = complete_nvr_req;
  1020. if (esas2r_nvram_write(a, rq, data)) {
  1021. /* now wait around for it to complete. */
  1022. while (!a->nvram_command_done)
  1023. wait_event_interruptible(a->nvram_waiter,
  1024. a->nvram_command_done);
  1025. ;
  1026. /* done, check the status. */
  1027. if (rq->req_stat == RS_SUCCESS)
  1028. result = 1;
  1029. }
  1030. return result;
  1031. }
  1032. /* This function only cares about ATTO-specific ioctls (atto_express_ioctl) */
  1033. int esas2r_ioctl_handler(void *hostdata, int cmd, void __user *arg)
  1034. {
  1035. struct atto_express_ioctl *ioctl = NULL;
  1036. struct esas2r_adapter *a;
  1037. struct esas2r_request *rq;
  1038. u16 code;
  1039. int err;
  1040. esas2r_log(ESAS2R_LOG_DEBG, "ioctl (%p, %x, %p)", hostdata, cmd, arg);
  1041. if ((arg == NULL)
  1042. || (cmd < EXPRESS_IOCTL_MIN)
  1043. || (cmd > EXPRESS_IOCTL_MAX))
  1044. return -ENOTSUPP;
  1045. ioctl = memdup_user(arg, sizeof(struct atto_express_ioctl));
  1046. if (IS_ERR(ioctl)) {
  1047. esas2r_log(ESAS2R_LOG_WARN,
  1048. "ioctl_handler access_ok failed for cmd %d, "
  1049. "address %p", cmd,
  1050. arg);
  1051. return PTR_ERR(ioctl);
  1052. }
  1053. /* verify the signature */
  1054. if (memcmp(ioctl->header.signature,
  1055. EXPRESS_IOCTL_SIGNATURE,
  1056. EXPRESS_IOCTL_SIGNATURE_SIZE) != 0) {
  1057. esas2r_log(ESAS2R_LOG_WARN, "invalid signature");
  1058. kfree(ioctl);
  1059. return -ENOTSUPP;
  1060. }
  1061. /* assume success */
  1062. ioctl->header.return_code = IOCTL_SUCCESS;
  1063. err = 0;
  1064. /*
  1065. * handle EXPRESS_IOCTL_GET_CHANNELS
  1066. * without paying attention to channel
  1067. */
  1068. if (cmd == EXPRESS_IOCTL_GET_CHANNELS) {
  1069. int i = 0, k = 0;
  1070. ioctl->data.chanlist.num_channels = 0;
  1071. while (i < MAX_ADAPTERS) {
  1072. if (esas2r_adapters[i]) {
  1073. ioctl->data.chanlist.num_channels++;
  1074. ioctl->data.chanlist.channel[k] = i;
  1075. k++;
  1076. }
  1077. i++;
  1078. }
  1079. goto ioctl_done;
  1080. }
  1081. /* get the channel */
  1082. if (ioctl->header.channel == 0xFF) {
  1083. a = (struct esas2r_adapter *)hostdata;
  1084. } else {
  1085. if (ioctl->header.channel >= MAX_ADAPTERS ||
  1086. esas2r_adapters[ioctl->header.channel] == NULL) {
  1087. ioctl->header.return_code = IOCTL_BAD_CHANNEL;
  1088. esas2r_log(ESAS2R_LOG_WARN, "bad channel value");
  1089. kfree(ioctl);
  1090. return -ENOTSUPP;
  1091. }
  1092. a = esas2r_adapters[ioctl->header.channel];
  1093. }
  1094. switch (cmd) {
  1095. case EXPRESS_IOCTL_RW_FIRMWARE:
  1096. if (ioctl->data.fwrw.img_type == FW_IMG_FM_API) {
  1097. err = esas2r_write_fw(a,
  1098. (char *)ioctl->data.fwrw.image,
  1099. 0,
  1100. sizeof(struct
  1101. atto_express_ioctl));
  1102. if (err >= 0) {
  1103. err = esas2r_read_fw(a,
  1104. (char *)ioctl->data.fwrw.
  1105. image,
  1106. 0,
  1107. sizeof(struct
  1108. atto_express_ioctl));
  1109. }
  1110. } else if (ioctl->data.fwrw.img_type == FW_IMG_FS_API) {
  1111. err = esas2r_write_fs(a,
  1112. (char *)ioctl->data.fwrw.image,
  1113. 0,
  1114. sizeof(struct
  1115. atto_express_ioctl));
  1116. if (err >= 0) {
  1117. err = esas2r_read_fs(a,
  1118. (char *)ioctl->data.fwrw.
  1119. image,
  1120. 0,
  1121. sizeof(struct
  1122. atto_express_ioctl));
  1123. }
  1124. } else {
  1125. ioctl->header.return_code = IOCTL_BAD_FLASH_IMGTYPE;
  1126. }
  1127. break;
  1128. case EXPRESS_IOCTL_READ_PARAMS:
  1129. memcpy(ioctl->data.prw.data_buffer, a->nvram,
  1130. sizeof(struct esas2r_sas_nvram));
  1131. ioctl->data.prw.code = 1;
  1132. break;
  1133. case EXPRESS_IOCTL_WRITE_PARAMS:
  1134. rq = esas2r_alloc_request(a);
  1135. if (rq == NULL) {
  1136. kfree(ioctl);
  1137. esas2r_log(ESAS2R_LOG_WARN,
  1138. "could not allocate an internal request");
  1139. return -ENOMEM;
  1140. }
  1141. code = esas2r_write_params(a, rq,
  1142. (struct esas2r_sas_nvram *)ioctl->data.prw.data_buffer);
  1143. ioctl->data.prw.code = code;
  1144. esas2r_free_request(a, rq);
  1145. break;
  1146. case EXPRESS_IOCTL_DEFAULT_PARAMS:
  1147. esas2r_nvram_get_defaults(a,
  1148. (struct esas2r_sas_nvram *)ioctl->data.prw.data_buffer);
  1149. ioctl->data.prw.code = 1;
  1150. break;
  1151. case EXPRESS_IOCTL_CHAN_INFO:
  1152. ioctl->data.chaninfo.major_rev = ESAS2R_MAJOR_REV;
  1153. ioctl->data.chaninfo.minor_rev = ESAS2R_MINOR_REV;
  1154. ioctl->data.chaninfo.IRQ = a->pcid->irq;
  1155. ioctl->data.chaninfo.device_id = a->pcid->device;
  1156. ioctl->data.chaninfo.vendor_id = a->pcid->vendor;
  1157. ioctl->data.chaninfo.ven_dev_id = a->pcid->subsystem_device;
  1158. ioctl->data.chaninfo.revision_id = a->pcid->revision;
  1159. ioctl->data.chaninfo.pci_bus = a->pcid->bus->number;
  1160. ioctl->data.chaninfo.pci_dev_func = a->pcid->devfn;
  1161. ioctl->data.chaninfo.core_rev = 0;
  1162. ioctl->data.chaninfo.host_no = a->host->host_no;
  1163. ioctl->data.chaninfo.hbaapi_rev = 0;
  1164. break;
  1165. case EXPRESS_IOCTL_SMP:
  1166. ioctl->header.return_code = handle_smp_ioctl(a,
  1167. &ioctl->data.
  1168. ioctl_smp);
  1169. break;
  1170. case EXPRESS_CSMI:
  1171. ioctl->header.return_code =
  1172. handle_csmi_ioctl(a, &ioctl->data.csmi);
  1173. break;
  1174. case EXPRESS_IOCTL_HBA:
  1175. ioctl->header.return_code = handle_hba_ioctl(a,
  1176. &ioctl->data.
  1177. ioctl_hba);
  1178. break;
  1179. case EXPRESS_IOCTL_VDA:
  1180. err = esas2r_write_vda(a,
  1181. (char *)&ioctl->data.ioctl_vda,
  1182. 0,
  1183. sizeof(struct atto_ioctl_vda) +
  1184. ioctl->data.ioctl_vda.data_length);
  1185. if (err >= 0) {
  1186. err = esas2r_read_vda(a,
  1187. (char *)&ioctl->data.ioctl_vda,
  1188. 0,
  1189. sizeof(struct atto_ioctl_vda) +
  1190. ioctl->data.ioctl_vda.data_length);
  1191. }
  1192. break;
  1193. case EXPRESS_IOCTL_GET_MOD_INFO:
  1194. ioctl->data.modinfo.adapter = a;
  1195. ioctl->data.modinfo.pci_dev = a->pcid;
  1196. ioctl->data.modinfo.scsi_host = a->host;
  1197. ioctl->data.modinfo.host_no = a->host->host_no;
  1198. break;
  1199. default:
  1200. esas2r_debug("esas2r_ioctl invalid cmd %p!", cmd);
  1201. ioctl->header.return_code = IOCTL_ERR_INVCMD;
  1202. }
  1203. ioctl_done:
  1204. if (err < 0) {
  1205. esas2r_log(ESAS2R_LOG_WARN, "err %d on ioctl cmd %d", err,
  1206. cmd);
  1207. switch (err) {
  1208. case -ENOMEM:
  1209. case -EBUSY:
  1210. ioctl->header.return_code = IOCTL_OUT_OF_RESOURCES;
  1211. break;
  1212. case -ENOSYS:
  1213. case -EINVAL:
  1214. ioctl->header.return_code = IOCTL_INVALID_PARAM;
  1215. break;
  1216. default:
  1217. ioctl->header.return_code = IOCTL_GENERAL_ERROR;
  1218. break;
  1219. }
  1220. }
  1221. /* Always copy the buffer back, if only to pick up the status */
  1222. err = __copy_to_user(arg, ioctl, sizeof(struct atto_express_ioctl));
  1223. if (err != 0) {
  1224. esas2r_log(ESAS2R_LOG_WARN,
  1225. "ioctl_handler copy_to_user didn't copy "
  1226. "everything (err %d, cmd %d)", err,
  1227. cmd);
  1228. kfree(ioctl);
  1229. return -EFAULT;
  1230. }
  1231. kfree(ioctl);
  1232. return 0;
  1233. }
  1234. int esas2r_ioctl(struct scsi_device *sd, int cmd, void __user *arg)
  1235. {
  1236. return esas2r_ioctl_handler(sd->host->hostdata, cmd, arg);
  1237. }
  1238. static void free_fw_buffers(struct esas2r_adapter *a)
  1239. {
  1240. if (a->firmware.data) {
  1241. dma_free_coherent(&a->pcid->dev,
  1242. (size_t)a->firmware.orig_len,
  1243. a->firmware.data,
  1244. (dma_addr_t)a->firmware.phys);
  1245. a->firmware.data = NULL;
  1246. }
  1247. }
  1248. static int allocate_fw_buffers(struct esas2r_adapter *a, u32 length)
  1249. {
  1250. free_fw_buffers(a);
  1251. a->firmware.orig_len = length;
  1252. a->firmware.data = (u8 *)dma_alloc_coherent(&a->pcid->dev,
  1253. (size_t)length,
  1254. (dma_addr_t *)&a->firmware.
  1255. phys,
  1256. GFP_KERNEL);
  1257. if (!a->firmware.data) {
  1258. esas2r_debug("buffer alloc failed!");
  1259. return 0;
  1260. }
  1261. return 1;
  1262. }
  1263. /* Handle a call to read firmware. */
  1264. int esas2r_read_fw(struct esas2r_adapter *a, char *buf, long off, int count)
  1265. {
  1266. esas2r_trace_enter();
  1267. /* if the cached header is a status, simply copy it over and return. */
  1268. if (a->firmware.state == FW_STATUS_ST) {
  1269. int size = min_t(int, count, sizeof(a->firmware.header));
  1270. esas2r_trace_exit();
  1271. memcpy(buf, &a->firmware.header, size);
  1272. esas2r_debug("esas2r_read_fw: STATUS size %d", size);
  1273. return size;
  1274. }
  1275. /*
  1276. * if the cached header is a command, do it if at
  1277. * offset 0, otherwise copy the pieces.
  1278. */
  1279. if (a->firmware.state == FW_COMMAND_ST) {
  1280. u32 length = a->firmware.header.length;
  1281. esas2r_trace_exit();
  1282. esas2r_debug("esas2r_read_fw: COMMAND length %d off %d",
  1283. length,
  1284. off);
  1285. if (off == 0) {
  1286. if (a->firmware.header.action == FI_ACT_UP) {
  1287. if (!allocate_fw_buffers(a, length))
  1288. return -ENOMEM;
  1289. /* copy header over */
  1290. memcpy(a->firmware.data,
  1291. &a->firmware.header,
  1292. sizeof(a->firmware.header));
  1293. do_fm_api(a,
  1294. (struct esas2r_flash_img *)a->firmware.data);
  1295. } else if (a->firmware.header.action == FI_ACT_UPSZ) {
  1296. int size =
  1297. min((int)count,
  1298. (int)sizeof(a->firmware.header));
  1299. do_fm_api(a, &a->firmware.header);
  1300. memcpy(buf, &a->firmware.header, size);
  1301. esas2r_debug("FI_ACT_UPSZ size %d", size);
  1302. return size;
  1303. } else {
  1304. esas2r_debug("invalid action %d",
  1305. a->firmware.header.action);
  1306. return -ENOSYS;
  1307. }
  1308. }
  1309. if (count + off > length)
  1310. count = length - off;
  1311. if (count < 0)
  1312. return 0;
  1313. if (!a->firmware.data) {
  1314. esas2r_debug(
  1315. "read: nonzero offset but no buffer available!");
  1316. return -ENOMEM;
  1317. }
  1318. esas2r_debug("esas2r_read_fw: off %d count %d length %d ", off,
  1319. count,
  1320. length);
  1321. memcpy(buf, &a->firmware.data[off], count);
  1322. /* when done, release the buffer */
  1323. if (length <= off + count) {
  1324. esas2r_debug("esas2r_read_fw: freeing buffer!");
  1325. free_fw_buffers(a);
  1326. }
  1327. return count;
  1328. }
  1329. esas2r_trace_exit();
  1330. esas2r_debug("esas2r_read_fw: invalid firmware state %d",
  1331. a->firmware.state);
  1332. return -EINVAL;
  1333. }
  1334. /* Handle a call to write firmware. */
  1335. int esas2r_write_fw(struct esas2r_adapter *a, const char *buf, long off,
  1336. int count)
  1337. {
  1338. u32 length;
  1339. if (off == 0) {
  1340. struct esas2r_flash_img *header =
  1341. (struct esas2r_flash_img *)buf;
  1342. /* assume version 0 flash image */
  1343. int min_size = sizeof(struct esas2r_flash_img_v0);
  1344. a->firmware.state = FW_INVALID_ST;
  1345. /* validate the version field first */
  1346. if (count < 4
  1347. || header->fi_version > FI_VERSION_1) {
  1348. esas2r_debug(
  1349. "esas2r_write_fw: short header or invalid version");
  1350. return -EINVAL;
  1351. }
  1352. /* See if its a version 1 flash image */
  1353. if (header->fi_version == FI_VERSION_1)
  1354. min_size = sizeof(struct esas2r_flash_img);
  1355. /* If this is the start, the header must be full and valid. */
  1356. if (count < min_size) {
  1357. esas2r_debug("esas2r_write_fw: short header, aborting");
  1358. return -EINVAL;
  1359. }
  1360. /* Make sure the size is reasonable. */
  1361. length = header->length;
  1362. if (length > 1024 * 1024) {
  1363. esas2r_debug(
  1364. "esas2r_write_fw: hosed, length %d fi_version %d",
  1365. length, header->fi_version);
  1366. return -EINVAL;
  1367. }
  1368. /*
  1369. * If this is a write command, allocate memory because
  1370. * we have to cache everything. otherwise, just cache
  1371. * the header, because the read op will do the command.
  1372. */
  1373. if (header->action == FI_ACT_DOWN) {
  1374. if (!allocate_fw_buffers(a, length))
  1375. return -ENOMEM;
  1376. /*
  1377. * Store the command, so there is context on subsequent
  1378. * calls.
  1379. */
  1380. memcpy(&a->firmware.header,
  1381. buf,
  1382. sizeof(*header));
  1383. } else if (header->action == FI_ACT_UP
  1384. || header->action == FI_ACT_UPSZ) {
  1385. /* Save the command, result will be picked up on read */
  1386. memcpy(&a->firmware.header,
  1387. buf,
  1388. sizeof(*header));
  1389. a->firmware.state = FW_COMMAND_ST;
  1390. esas2r_debug(
  1391. "esas2r_write_fw: COMMAND, count %d, action %d ",
  1392. count, header->action);
  1393. /*
  1394. * Pretend we took the whole buffer,
  1395. * so we don't get bothered again.
  1396. */
  1397. return count;
  1398. } else {
  1399. esas2r_debug("esas2r_write_fw: invalid action %d ",
  1400. a->firmware.header.action);
  1401. return -ENOSYS;
  1402. }
  1403. } else {
  1404. length = a->firmware.header.length;
  1405. }
  1406. /*
  1407. * We only get here on a download command, regardless of offset.
  1408. * the chunks written by the system need to be cached, and when
  1409. * the final one arrives, issue the fmapi command.
  1410. */
  1411. if (off + count > length)
  1412. count = length - off;
  1413. if (count > 0) {
  1414. esas2r_debug("esas2r_write_fw: off %d count %d length %d", off,
  1415. count,
  1416. length);
  1417. /*
  1418. * On a full upload, the system tries sending the whole buffer.
  1419. * there's nothing to do with it, so just drop it here, before
  1420. * trying to copy over into unallocated memory!
  1421. */
  1422. if (a->firmware.header.action == FI_ACT_UP)
  1423. return count;
  1424. if (!a->firmware.data) {
  1425. esas2r_debug(
  1426. "write: nonzero offset but no buffer available!");
  1427. return -ENOMEM;
  1428. }
  1429. memcpy(&a->firmware.data[off], buf, count);
  1430. if (length == off + count) {
  1431. do_fm_api(a,
  1432. (struct esas2r_flash_img *)a->firmware.data);
  1433. /*
  1434. * Now copy the header result to be picked up by the
  1435. * next read
  1436. */
  1437. memcpy(&a->firmware.header,
  1438. a->firmware.data,
  1439. sizeof(a->firmware.header));
  1440. a->firmware.state = FW_STATUS_ST;
  1441. esas2r_debug("write completed");
  1442. /*
  1443. * Since the system has the data buffered, the only way
  1444. * this can leak is if a root user writes a program
  1445. * that writes a shorter buffer than it claims, and the
  1446. * copyin fails.
  1447. */
  1448. free_fw_buffers(a);
  1449. }
  1450. }
  1451. return count;
  1452. }
  1453. /* Callback for the completion of a VDA request. */
  1454. static void vda_complete_req(struct esas2r_adapter *a,
  1455. struct esas2r_request *rq)
  1456. {
  1457. a->vda_command_done = 1;
  1458. wake_up_interruptible(&a->vda_waiter);
  1459. }
  1460. /* Scatter/gather callback for VDA requests */
  1461. static u32 get_physaddr_vda(struct esas2r_sg_context *sgc, u64 *addr)
  1462. {
  1463. struct esas2r_adapter *a = (struct esas2r_adapter *)sgc->adapter;
  1464. int offset = (u8 *)sgc->cur_offset - (u8 *)a->vda_buffer;
  1465. (*addr) = a->ppvda_buffer + offset;
  1466. return VDA_MAX_BUFFER_SIZE - offset;
  1467. }
  1468. /* Handle a call to read a VDA command. */
  1469. int esas2r_read_vda(struct esas2r_adapter *a, char *buf, long off, int count)
  1470. {
  1471. if (!a->vda_buffer)
  1472. return -ENOMEM;
  1473. if (off == 0) {
  1474. struct esas2r_request *rq;
  1475. struct atto_ioctl_vda *vi =
  1476. (struct atto_ioctl_vda *)a->vda_buffer;
  1477. struct esas2r_sg_context sgc;
  1478. bool wait_for_completion;
  1479. /*
  1480. * Presumeably, someone has already written to the vda_buffer,
  1481. * and now they are reading the node the response, so now we
  1482. * will actually issue the request to the chip and reply.
  1483. */
  1484. /* allocate a request */
  1485. rq = esas2r_alloc_request(a);
  1486. if (rq == NULL) {
  1487. esas2r_debug("esas2r_read_vda: out of requests");
  1488. return -EBUSY;
  1489. }
  1490. rq->comp_cb = vda_complete_req;
  1491. sgc.first_req = rq;
  1492. sgc.adapter = a;
  1493. sgc.cur_offset = a->vda_buffer + VDA_BUFFER_HEADER_SZ;
  1494. sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_vda;
  1495. a->vda_command_done = 0;
  1496. wait_for_completion =
  1497. esas2r_process_vda_ioctl(a, vi, rq, &sgc);
  1498. if (wait_for_completion) {
  1499. /* now wait around for it to complete. */
  1500. while (!a->vda_command_done)
  1501. wait_event_interruptible(a->vda_waiter,
  1502. a->vda_command_done);
  1503. }
  1504. esas2r_free_request(a, (struct esas2r_request *)rq);
  1505. }
  1506. if (off > VDA_MAX_BUFFER_SIZE)
  1507. return 0;
  1508. if (count + off > VDA_MAX_BUFFER_SIZE)
  1509. count = VDA_MAX_BUFFER_SIZE - off;
  1510. if (count < 0)
  1511. return 0;
  1512. memcpy(buf, a->vda_buffer + off, count);
  1513. return count;
  1514. }
  1515. /* Handle a call to write a VDA command. */
  1516. int esas2r_write_vda(struct esas2r_adapter *a, const char *buf, long off,
  1517. int count)
  1518. {
  1519. /*
  1520. * allocate memory for it, if not already done. once allocated,
  1521. * we will keep it around until the driver is unloaded.
  1522. */
  1523. if (!a->vda_buffer) {
  1524. dma_addr_t dma_addr;
  1525. a->vda_buffer = (u8 *)dma_alloc_coherent(&a->pcid->dev,
  1526. (size_t)
  1527. VDA_MAX_BUFFER_SIZE,
  1528. &dma_addr,
  1529. GFP_KERNEL);
  1530. a->ppvda_buffer = dma_addr;
  1531. }
  1532. if (!a->vda_buffer)
  1533. return -ENOMEM;
  1534. if (off > VDA_MAX_BUFFER_SIZE)
  1535. return 0;
  1536. if (count + off > VDA_MAX_BUFFER_SIZE)
  1537. count = VDA_MAX_BUFFER_SIZE - off;
  1538. if (count < 1)
  1539. return 0;
  1540. memcpy(a->vda_buffer + off, buf, count);
  1541. return count;
  1542. }
  1543. /* Callback for the completion of an FS_API request.*/
  1544. static void fs_api_complete_req(struct esas2r_adapter *a,
  1545. struct esas2r_request *rq)
  1546. {
  1547. a->fs_api_command_done = 1;
  1548. wake_up_interruptible(&a->fs_api_waiter);
  1549. }
  1550. /* Scatter/gather callback for VDA requests */
  1551. static u32 get_physaddr_fs_api(struct esas2r_sg_context *sgc, u64 *addr)
  1552. {
  1553. struct esas2r_adapter *a = (struct esas2r_adapter *)sgc->adapter;
  1554. struct esas2r_ioctl_fs *fs =
  1555. (struct esas2r_ioctl_fs *)a->fs_api_buffer;
  1556. u32 offset = (u8 *)sgc->cur_offset - (u8 *)fs;
  1557. (*addr) = a->ppfs_api_buffer + offset;
  1558. return a->fs_api_buffer_size - offset;
  1559. }
  1560. /* Handle a call to read firmware via FS_API. */
  1561. int esas2r_read_fs(struct esas2r_adapter *a, char *buf, long off, int count)
  1562. {
  1563. if (!a->fs_api_buffer)
  1564. return -ENOMEM;
  1565. if (off == 0) {
  1566. struct esas2r_request *rq;
  1567. struct esas2r_sg_context sgc;
  1568. struct esas2r_ioctl_fs *fs =
  1569. (struct esas2r_ioctl_fs *)a->fs_api_buffer;
  1570. /* If another flash request is already in progress, return. */
  1571. if (mutex_lock_interruptible(&a->fs_api_mutex)) {
  1572. busy:
  1573. fs->status = ATTO_STS_OUT_OF_RSRC;
  1574. return -EBUSY;
  1575. }
  1576. /*
  1577. * Presumeably, someone has already written to the
  1578. * fs_api_buffer, and now they are reading the node the
  1579. * response, so now we will actually issue the request to the
  1580. * chip and reply. Allocate a request
  1581. */
  1582. rq = esas2r_alloc_request(a);
  1583. if (rq == NULL) {
  1584. esas2r_debug("esas2r_read_fs: out of requests");
  1585. mutex_unlock(&a->fs_api_mutex);
  1586. goto busy;
  1587. }
  1588. rq->comp_cb = fs_api_complete_req;
  1589. /* Set up the SGCONTEXT for to build the s/g table */
  1590. sgc.cur_offset = fs->data;
  1591. sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_fs_api;
  1592. a->fs_api_command_done = 0;
  1593. if (!esas2r_process_fs_ioctl(a, fs, rq, &sgc)) {
  1594. if (fs->status == ATTO_STS_OUT_OF_RSRC)
  1595. count = -EBUSY;
  1596. goto dont_wait;
  1597. }
  1598. /* Now wait around for it to complete. */
  1599. while (!a->fs_api_command_done)
  1600. wait_event_interruptible(a->fs_api_waiter,
  1601. a->fs_api_command_done);
  1602. ;
  1603. dont_wait:
  1604. /* Free the request and keep going */
  1605. mutex_unlock(&a->fs_api_mutex);
  1606. esas2r_free_request(a, (struct esas2r_request *)rq);
  1607. /* Pick up possible error code from above */
  1608. if (count < 0)
  1609. return count;
  1610. }
  1611. if (off > a->fs_api_buffer_size)
  1612. return 0;
  1613. if (count + off > a->fs_api_buffer_size)
  1614. count = a->fs_api_buffer_size - off;
  1615. if (count < 0)
  1616. return 0;
  1617. memcpy(buf, a->fs_api_buffer + off, count);
  1618. return count;
  1619. }
  1620. /* Handle a call to write firmware via FS_API. */
  1621. int esas2r_write_fs(struct esas2r_adapter *a, const char *buf, long off,
  1622. int count)
  1623. {
  1624. if (off == 0) {
  1625. struct esas2r_ioctl_fs *fs = (struct esas2r_ioctl_fs *)buf;
  1626. u32 length = fs->command.length + offsetof(
  1627. struct esas2r_ioctl_fs,
  1628. data);
  1629. /*
  1630. * Special case, for BEGIN commands, the length field
  1631. * is lying to us, so just get enough for the header.
  1632. */
  1633. if (fs->command.command == ESAS2R_FS_CMD_BEGINW)
  1634. length = offsetof(struct esas2r_ioctl_fs, data);
  1635. /*
  1636. * Beginning a command. We assume we'll get at least
  1637. * enough in the first write so we can look at the
  1638. * header and see how much we need to alloc.
  1639. */
  1640. if (count < offsetof(struct esas2r_ioctl_fs, data))
  1641. return -EINVAL;
  1642. /* Allocate a buffer or use the existing buffer. */
  1643. if (a->fs_api_buffer) {
  1644. if (a->fs_api_buffer_size < length) {
  1645. /* Free too-small buffer and get a new one */
  1646. dma_free_coherent(&a->pcid->dev,
  1647. (size_t)a->fs_api_buffer_size,
  1648. a->fs_api_buffer,
  1649. (dma_addr_t)a->ppfs_api_buffer);
  1650. goto re_allocate_buffer;
  1651. }
  1652. } else {
  1653. re_allocate_buffer:
  1654. a->fs_api_buffer_size = length;
  1655. a->fs_api_buffer = (u8 *)dma_alloc_coherent(
  1656. &a->pcid->dev,
  1657. (size_t)a->fs_api_buffer_size,
  1658. (dma_addr_t *)&a->ppfs_api_buffer,
  1659. GFP_KERNEL);
  1660. }
  1661. }
  1662. if (!a->fs_api_buffer)
  1663. return -ENOMEM;
  1664. if (off > a->fs_api_buffer_size)
  1665. return 0;
  1666. if (count + off > a->fs_api_buffer_size)
  1667. count = a->fs_api_buffer_size - off;
  1668. if (count < 1)
  1669. return 0;
  1670. memcpy(a->fs_api_buffer + off, buf, count);
  1671. return count;
  1672. }