mpt3sas_transport.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133
  1. /*
  2. * SAS Transport Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt3sas/mpt3sas_transport.c
  5. * Copyright (C) 2012-2014 LSI Corporation
  6. * Copyright (C) 2013-2014 Avago Technologies
  7. * (mailto: MPT-FusionLinux.pdl@avagotech.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/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/sched.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/delay.h>
  49. #include <linux/pci.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_device.h>
  53. #include <scsi/scsi_host.h>
  54. #include <scsi/scsi_transport_sas.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include "mpt3sas_base.h"
  57. /**
  58. * _transport_sas_node_find_by_sas_address - sas node search
  59. * @ioc: per adapter object
  60. * @sas_address: sas address of expander or sas host
  61. * Context: Calling function should acquire ioc->sas_node_lock.
  62. *
  63. * Search for either hba phys or expander device based on handle, then returns
  64. * the sas_node object.
  65. */
  66. static struct _sas_node *
  67. _transport_sas_node_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
  68. u64 sas_address)
  69. {
  70. if (ioc->sas_hba.sas_address == sas_address)
  71. return &ioc->sas_hba;
  72. else
  73. return mpt3sas_scsih_expander_find_by_sas_address(ioc,
  74. sas_address);
  75. }
  76. /**
  77. * _transport_convert_phy_link_rate -
  78. * @link_rate: link rate returned from mpt firmware
  79. *
  80. * Convert link_rate from mpi fusion into sas_transport form.
  81. */
  82. static enum sas_linkrate
  83. _transport_convert_phy_link_rate(u8 link_rate)
  84. {
  85. enum sas_linkrate rc;
  86. switch (link_rate) {
  87. case MPI2_SAS_NEG_LINK_RATE_1_5:
  88. rc = SAS_LINK_RATE_1_5_GBPS;
  89. break;
  90. case MPI2_SAS_NEG_LINK_RATE_3_0:
  91. rc = SAS_LINK_RATE_3_0_GBPS;
  92. break;
  93. case MPI2_SAS_NEG_LINK_RATE_6_0:
  94. rc = SAS_LINK_RATE_6_0_GBPS;
  95. break;
  96. case MPI25_SAS_NEG_LINK_RATE_12_0:
  97. rc = SAS_LINK_RATE_12_0_GBPS;
  98. break;
  99. case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
  100. rc = SAS_PHY_DISABLED;
  101. break;
  102. case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
  103. rc = SAS_LINK_RATE_FAILED;
  104. break;
  105. case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
  106. rc = SAS_SATA_PORT_SELECTOR;
  107. break;
  108. case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
  109. rc = SAS_PHY_RESET_IN_PROGRESS;
  110. break;
  111. default:
  112. case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
  113. case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
  114. rc = SAS_LINK_RATE_UNKNOWN;
  115. break;
  116. }
  117. return rc;
  118. }
  119. /**
  120. * _transport_set_identify - set identify for phys and end devices
  121. * @ioc: per adapter object
  122. * @handle: device handle
  123. * @identify: sas identify info
  124. *
  125. * Populates sas identify info.
  126. *
  127. * Returns 0 for success, non-zero for failure.
  128. */
  129. static int
  130. _transport_set_identify(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  131. struct sas_identify *identify)
  132. {
  133. Mpi2SasDevicePage0_t sas_device_pg0;
  134. Mpi2ConfigReply_t mpi_reply;
  135. u32 device_info;
  136. u32 ioc_status;
  137. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  138. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  139. __func__, ioc->name);
  140. return -EFAULT;
  141. }
  142. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  143. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  144. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  145. ioc->name, __FILE__, __LINE__, __func__);
  146. return -ENXIO;
  147. }
  148. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  149. MPI2_IOCSTATUS_MASK;
  150. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  151. pr_err(MPT3SAS_FMT
  152. "handle(0x%04x), ioc_status(0x%04x)\nfailure at %s:%d/%s()!\n",
  153. ioc->name, handle, ioc_status,
  154. __FILE__, __LINE__, __func__);
  155. return -EIO;
  156. }
  157. memset(identify, 0, sizeof(struct sas_identify));
  158. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  159. /* sas_address */
  160. identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  161. /* phy number of the parent device this device is linked to */
  162. identify->phy_identifier = sas_device_pg0.PhyNum;
  163. /* device_type */
  164. switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  165. case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
  166. identify->device_type = SAS_PHY_UNUSED;
  167. break;
  168. case MPI2_SAS_DEVICE_INFO_END_DEVICE:
  169. identify->device_type = SAS_END_DEVICE;
  170. break;
  171. case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
  172. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  173. break;
  174. case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  175. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  176. break;
  177. }
  178. /* initiator_port_protocols */
  179. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
  180. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  181. if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
  182. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  183. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
  184. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  185. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
  186. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  187. /* target_port_protocols */
  188. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
  189. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  190. if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  191. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  192. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
  193. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  194. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  195. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  196. return 0;
  197. }
  198. /**
  199. * mpt3sas_transport_done - internal transport layer callback handler.
  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. *
  205. * Callback handler when sending internal generated transport cmds.
  206. * The callback index passed is `ioc->transport_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. mpt3sas_transport_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  213. u32 reply)
  214. {
  215. MPI2DefaultReply_t *mpi_reply;
  216. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  217. if (ioc->transport_cmds.status == MPT3_CMD_NOT_USED)
  218. return 1;
  219. if (ioc->transport_cmds.smid != smid)
  220. return 1;
  221. ioc->transport_cmds.status |= MPT3_CMD_COMPLETE;
  222. if (mpi_reply) {
  223. memcpy(ioc->transport_cmds.reply, mpi_reply,
  224. mpi_reply->MsgLength*4);
  225. ioc->transport_cmds.status |= MPT3_CMD_REPLY_VALID;
  226. }
  227. ioc->transport_cmds.status &= ~MPT3_CMD_PENDING;
  228. complete(&ioc->transport_cmds.done);
  229. return 1;
  230. }
  231. /* report manufacture request structure */
  232. struct rep_manu_request {
  233. u8 smp_frame_type;
  234. u8 function;
  235. u8 reserved;
  236. u8 request_length;
  237. };
  238. /* report manufacture reply structure */
  239. struct rep_manu_reply {
  240. u8 smp_frame_type; /* 0x41 */
  241. u8 function; /* 0x01 */
  242. u8 function_result;
  243. u8 response_length;
  244. u16 expander_change_count;
  245. u8 reserved0[2];
  246. u8 sas_format;
  247. u8 reserved2[3];
  248. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  249. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  250. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  251. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  252. u16 component_id;
  253. u8 component_revision_id;
  254. u8 reserved3;
  255. u8 vendor_specific[8];
  256. };
  257. /**
  258. * transport_expander_report_manufacture - obtain SMP report_manufacture
  259. * @ioc: per adapter object
  260. * @sas_address: expander sas address
  261. * @edev: the sas_expander_device object
  262. *
  263. * Fills in the sas_expander_device object when SMP port is created.
  264. *
  265. * Returns 0 for success, non-zero for failure.
  266. */
  267. static int
  268. _transport_expander_report_manufacture(struct MPT3SAS_ADAPTER *ioc,
  269. u64 sas_address, struct sas_expander_device *edev)
  270. {
  271. Mpi2SmpPassthroughRequest_t *mpi_request;
  272. Mpi2SmpPassthroughReply_t *mpi_reply;
  273. struct rep_manu_reply *manufacture_reply;
  274. struct rep_manu_request *manufacture_request;
  275. int rc;
  276. u16 smid;
  277. u32 ioc_state;
  278. void *psge;
  279. u8 issue_reset = 0;
  280. void *data_out = NULL;
  281. dma_addr_t data_out_dma;
  282. dma_addr_t data_in_dma;
  283. size_t data_in_sz;
  284. size_t data_out_sz;
  285. u16 wait_state_count;
  286. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  287. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  288. __func__, ioc->name);
  289. return -EFAULT;
  290. }
  291. mutex_lock(&ioc->transport_cmds.mutex);
  292. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  293. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  294. ioc->name, __func__);
  295. rc = -EAGAIN;
  296. goto out;
  297. }
  298. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  299. wait_state_count = 0;
  300. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  301. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  302. if (wait_state_count++ == 10) {
  303. pr_err(MPT3SAS_FMT
  304. "%s: failed due to ioc not operational\n",
  305. ioc->name, __func__);
  306. rc = -EFAULT;
  307. goto out;
  308. }
  309. ssleep(1);
  310. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  311. pr_info(MPT3SAS_FMT
  312. "%s: waiting for operational state(count=%d)\n",
  313. ioc->name, __func__, wait_state_count);
  314. }
  315. if (wait_state_count)
  316. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  317. ioc->name, __func__);
  318. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  319. if (!smid) {
  320. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  321. ioc->name, __func__);
  322. rc = -EAGAIN;
  323. goto out;
  324. }
  325. rc = 0;
  326. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  327. ioc->transport_cmds.smid = smid;
  328. data_out_sz = sizeof(struct rep_manu_request);
  329. data_in_sz = sizeof(struct rep_manu_reply);
  330. data_out = pci_alloc_consistent(ioc->pdev, data_out_sz + data_in_sz,
  331. &data_out_dma);
  332. if (!data_out) {
  333. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  334. __LINE__, __func__);
  335. rc = -ENOMEM;
  336. mpt3sas_base_free_smid(ioc, smid);
  337. goto out;
  338. }
  339. data_in_dma = data_out_dma + sizeof(struct rep_manu_request);
  340. manufacture_request = data_out;
  341. manufacture_request->smp_frame_type = 0x40;
  342. manufacture_request->function = 1;
  343. manufacture_request->reserved = 0;
  344. manufacture_request->request_length = 0;
  345. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  346. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  347. mpi_request->PhysicalPort = 0xFF;
  348. mpi_request->SASAddress = cpu_to_le64(sas_address);
  349. mpi_request->RequestDataLength = cpu_to_le16(data_out_sz);
  350. psge = &mpi_request->SGL;
  351. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  352. data_in_sz);
  353. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  354. "report_manufacture - send to sas_addr(0x%016llx)\n",
  355. ioc->name, (unsigned long long)sas_address));
  356. init_completion(&ioc->transport_cmds.done);
  357. mpt3sas_base_put_smid_default(ioc, smid);
  358. wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
  359. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  360. pr_err(MPT3SAS_FMT "%s: timeout\n",
  361. ioc->name, __func__);
  362. _debug_dump_mf(mpi_request,
  363. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  364. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  365. issue_reset = 1;
  366. goto issue_host_reset;
  367. }
  368. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  369. "report_manufacture - complete\n", ioc->name));
  370. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  371. u8 *tmp;
  372. mpi_reply = ioc->transport_cmds.reply;
  373. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  374. "report_manufacture - reply data transfer size(%d)\n",
  375. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  376. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  377. sizeof(struct rep_manu_reply))
  378. goto out;
  379. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  380. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  381. SAS_EXPANDER_VENDOR_ID_LEN);
  382. strncpy(edev->product_id, manufacture_reply->product_id,
  383. SAS_EXPANDER_PRODUCT_ID_LEN);
  384. strncpy(edev->product_rev, manufacture_reply->product_rev,
  385. SAS_EXPANDER_PRODUCT_REV_LEN);
  386. edev->level = manufacture_reply->sas_format & 1;
  387. if (edev->level) {
  388. strncpy(edev->component_vendor_id,
  389. manufacture_reply->component_vendor_id,
  390. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  391. tmp = (u8 *)&manufacture_reply->component_id;
  392. edev->component_id = tmp[0] << 8 | tmp[1];
  393. edev->component_revision_id =
  394. manufacture_reply->component_revision_id;
  395. }
  396. } else
  397. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  398. "report_manufacture - no reply\n", ioc->name));
  399. issue_host_reset:
  400. if (issue_reset)
  401. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  402. out:
  403. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  404. if (data_out)
  405. pci_free_consistent(ioc->pdev, data_out_sz + data_in_sz,
  406. data_out, data_out_dma);
  407. mutex_unlock(&ioc->transport_cmds.mutex);
  408. return rc;
  409. }
  410. /**
  411. * _transport_delete_port - helper function to removing a port
  412. * @ioc: per adapter object
  413. * @mpt3sas_port: mpt3sas per port object
  414. *
  415. * Returns nothing.
  416. */
  417. static void
  418. _transport_delete_port(struct MPT3SAS_ADAPTER *ioc,
  419. struct _sas_port *mpt3sas_port)
  420. {
  421. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  422. enum sas_device_type device_type =
  423. mpt3sas_port->remote_identify.device_type;
  424. dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
  425. "remove: sas_addr(0x%016llx)\n",
  426. (unsigned long long) sas_address);
  427. ioc->logging_level |= MPT_DEBUG_TRANSPORT;
  428. if (device_type == SAS_END_DEVICE)
  429. mpt3sas_device_remove_by_sas_address(ioc, sas_address);
  430. else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
  431. device_type == SAS_FANOUT_EXPANDER_DEVICE)
  432. mpt3sas_expander_remove(ioc, sas_address);
  433. ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
  434. }
  435. /**
  436. * _transport_delete_phy - helper function to removing single phy from port
  437. * @ioc: per adapter object
  438. * @mpt3sas_port: mpt3sas per port object
  439. * @mpt3sas_phy: mpt3sas per phy object
  440. *
  441. * Returns nothing.
  442. */
  443. static void
  444. _transport_delete_phy(struct MPT3SAS_ADAPTER *ioc,
  445. struct _sas_port *mpt3sas_port, struct _sas_phy *mpt3sas_phy)
  446. {
  447. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  448. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  449. "remove: sas_addr(0x%016llx), phy(%d)\n",
  450. (unsigned long long) sas_address, mpt3sas_phy->phy_id);
  451. list_del(&mpt3sas_phy->port_siblings);
  452. mpt3sas_port->num_phys--;
  453. sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  454. mpt3sas_phy->phy_belongs_to_port = 0;
  455. }
  456. /**
  457. * _transport_add_phy - helper function to adding single phy to port
  458. * @ioc: per adapter object
  459. * @mpt3sas_port: mpt3sas per port object
  460. * @mpt3sas_phy: mpt3sas per phy object
  461. *
  462. * Returns nothing.
  463. */
  464. static void
  465. _transport_add_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_port *mpt3sas_port,
  466. struct _sas_phy *mpt3sas_phy)
  467. {
  468. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  469. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  470. "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
  471. sas_address, mpt3sas_phy->phy_id);
  472. list_add_tail(&mpt3sas_phy->port_siblings, &mpt3sas_port->phy_list);
  473. mpt3sas_port->num_phys++;
  474. sas_port_add_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  475. mpt3sas_phy->phy_belongs_to_port = 1;
  476. }
  477. /**
  478. * _transport_add_phy_to_an_existing_port - adding new phy to existing port
  479. * @ioc: per adapter object
  480. * @sas_node: sas node object (either expander or sas host)
  481. * @mpt3sas_phy: mpt3sas per phy object
  482. * @sas_address: sas address of device/expander were phy needs to be added to
  483. *
  484. * Returns nothing.
  485. */
  486. static void
  487. _transport_add_phy_to_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
  488. struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy,
  489. u64 sas_address)
  490. {
  491. struct _sas_port *mpt3sas_port;
  492. struct _sas_phy *phy_srch;
  493. if (mpt3sas_phy->phy_belongs_to_port == 1)
  494. return;
  495. list_for_each_entry(mpt3sas_port, &sas_node->sas_port_list,
  496. port_list) {
  497. if (mpt3sas_port->remote_identify.sas_address !=
  498. sas_address)
  499. continue;
  500. list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
  501. port_siblings) {
  502. if (phy_srch == mpt3sas_phy)
  503. return;
  504. }
  505. _transport_add_phy(ioc, mpt3sas_port, mpt3sas_phy);
  506. return;
  507. }
  508. }
  509. /**
  510. * _transport_del_phy_from_an_existing_port - delete phy from existing port
  511. * @ioc: per adapter object
  512. * @sas_node: sas node object (either expander or sas host)
  513. * @mpt3sas_phy: mpt3sas per phy object
  514. *
  515. * Returns nothing.
  516. */
  517. static void
  518. _transport_del_phy_from_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
  519. struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy)
  520. {
  521. struct _sas_port *mpt3sas_port, *next;
  522. struct _sas_phy *phy_srch;
  523. if (mpt3sas_phy->phy_belongs_to_port == 0)
  524. return;
  525. list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
  526. port_list) {
  527. list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
  528. port_siblings) {
  529. if (phy_srch != mpt3sas_phy)
  530. continue;
  531. if (mpt3sas_port->num_phys == 1)
  532. _transport_delete_port(ioc, mpt3sas_port);
  533. else
  534. _transport_delete_phy(ioc, mpt3sas_port,
  535. mpt3sas_phy);
  536. return;
  537. }
  538. }
  539. }
  540. /**
  541. * _transport_sanity_check - sanity check when adding a new port
  542. * @ioc: per adapter object
  543. * @sas_node: sas node object (either expander or sas host)
  544. * @sas_address: sas address of device being added
  545. *
  546. * See the explanation above from _transport_delete_duplicate_port
  547. */
  548. static void
  549. _transport_sanity_check(struct MPT3SAS_ADAPTER *ioc, struct _sas_node *sas_node,
  550. u64 sas_address)
  551. {
  552. int i;
  553. for (i = 0; i < sas_node->num_phys; i++) {
  554. if (sas_node->phy[i].remote_identify.sas_address != sas_address)
  555. continue;
  556. if (sas_node->phy[i].phy_belongs_to_port == 1)
  557. _transport_del_phy_from_an_existing_port(ioc, sas_node,
  558. &sas_node->phy[i]);
  559. }
  560. }
  561. /**
  562. * mpt3sas_transport_port_add - insert port to the list
  563. * @ioc: per adapter object
  564. * @handle: handle of attached device
  565. * @sas_address: sas address of parent expander or sas host
  566. * Context: This function will acquire ioc->sas_node_lock.
  567. *
  568. * Adding new port object to the sas_node->sas_port_list.
  569. *
  570. * Returns mpt3sas_port.
  571. */
  572. struct _sas_port *
  573. mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  574. u64 sas_address)
  575. {
  576. struct _sas_phy *mpt3sas_phy, *next;
  577. struct _sas_port *mpt3sas_port;
  578. unsigned long flags;
  579. struct _sas_node *sas_node;
  580. struct sas_rphy *rphy;
  581. struct _sas_device *sas_device = NULL;
  582. int i;
  583. struct sas_port *port;
  584. mpt3sas_port = kzalloc(sizeof(struct _sas_port),
  585. GFP_KERNEL);
  586. if (!mpt3sas_port) {
  587. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  588. ioc->name, __FILE__, __LINE__, __func__);
  589. return NULL;
  590. }
  591. INIT_LIST_HEAD(&mpt3sas_port->port_list);
  592. INIT_LIST_HEAD(&mpt3sas_port->phy_list);
  593. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  594. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  595. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  596. if (!sas_node) {
  597. pr_err(MPT3SAS_FMT
  598. "%s: Could not find parent sas_address(0x%016llx)!\n",
  599. ioc->name, __func__, (unsigned long long)sas_address);
  600. goto out_fail;
  601. }
  602. if ((_transport_set_identify(ioc, handle,
  603. &mpt3sas_port->remote_identify))) {
  604. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  605. ioc->name, __FILE__, __LINE__, __func__);
  606. goto out_fail;
  607. }
  608. if (mpt3sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
  609. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  610. ioc->name, __FILE__, __LINE__, __func__);
  611. goto out_fail;
  612. }
  613. _transport_sanity_check(ioc, sas_node,
  614. mpt3sas_port->remote_identify.sas_address);
  615. for (i = 0; i < sas_node->num_phys; i++) {
  616. if (sas_node->phy[i].remote_identify.sas_address !=
  617. mpt3sas_port->remote_identify.sas_address)
  618. continue;
  619. list_add_tail(&sas_node->phy[i].port_siblings,
  620. &mpt3sas_port->phy_list);
  621. mpt3sas_port->num_phys++;
  622. }
  623. if (!mpt3sas_port->num_phys) {
  624. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  625. ioc->name, __FILE__, __LINE__, __func__);
  626. goto out_fail;
  627. }
  628. if (!sas_node->parent_dev) {
  629. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  630. ioc->name, __FILE__, __LINE__, __func__);
  631. goto out_fail;
  632. }
  633. port = sas_port_alloc_num(sas_node->parent_dev);
  634. if ((sas_port_add(port))) {
  635. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  636. ioc->name, __FILE__, __LINE__, __func__);
  637. goto out_fail;
  638. }
  639. list_for_each_entry(mpt3sas_phy, &mpt3sas_port->phy_list,
  640. port_siblings) {
  641. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  642. dev_printk(KERN_INFO, &port->dev,
  643. "add: handle(0x%04x), sas_addr(0x%016llx), phy(%d)\n",
  644. handle, (unsigned long long)
  645. mpt3sas_port->remote_identify.sas_address,
  646. mpt3sas_phy->phy_id);
  647. sas_port_add_phy(port, mpt3sas_phy->phy);
  648. mpt3sas_phy->phy_belongs_to_port = 1;
  649. }
  650. mpt3sas_port->port = port;
  651. if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE)
  652. rphy = sas_end_device_alloc(port);
  653. else
  654. rphy = sas_expander_alloc(port,
  655. mpt3sas_port->remote_identify.device_type);
  656. rphy->identify = mpt3sas_port->remote_identify;
  657. if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
  658. sas_device = mpt3sas_get_sdev_by_addr(ioc,
  659. mpt3sas_port->remote_identify.sas_address);
  660. if (!sas_device) {
  661. dfailprintk(ioc, printk(MPT3SAS_FMT
  662. "failure at %s:%d/%s()!\n",
  663. ioc->name, __FILE__, __LINE__, __func__));
  664. goto out_fail;
  665. }
  666. sas_device->pend_sas_rphy_add = 1;
  667. }
  668. if ((sas_rphy_add(rphy))) {
  669. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  670. ioc->name, __FILE__, __LINE__, __func__);
  671. }
  672. if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
  673. sas_device->pend_sas_rphy_add = 0;
  674. sas_device_put(sas_device);
  675. }
  676. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  677. dev_printk(KERN_INFO, &rphy->dev,
  678. "add: handle(0x%04x), sas_addr(0x%016llx)\n",
  679. handle, (unsigned long long)
  680. mpt3sas_port->remote_identify.sas_address);
  681. mpt3sas_port->rphy = rphy;
  682. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  683. list_add_tail(&mpt3sas_port->port_list, &sas_node->sas_port_list);
  684. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  685. /* fill in report manufacture */
  686. if (mpt3sas_port->remote_identify.device_type ==
  687. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  688. mpt3sas_port->remote_identify.device_type ==
  689. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
  690. _transport_expander_report_manufacture(ioc,
  691. mpt3sas_port->remote_identify.sas_address,
  692. rphy_to_expander_device(rphy));
  693. return mpt3sas_port;
  694. out_fail:
  695. list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list,
  696. port_siblings)
  697. list_del(&mpt3sas_phy->port_siblings);
  698. kfree(mpt3sas_port);
  699. return NULL;
  700. }
  701. /**
  702. * mpt3sas_transport_port_remove - remove port from the list
  703. * @ioc: per adapter object
  704. * @sas_address: sas address of attached device
  705. * @sas_address_parent: sas address of parent expander or sas host
  706. * Context: This function will acquire ioc->sas_node_lock.
  707. *
  708. * Removing object and freeing associated memory from the
  709. * ioc->sas_port_list.
  710. *
  711. * Return nothing.
  712. */
  713. void
  714. mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
  715. u64 sas_address_parent)
  716. {
  717. int i;
  718. unsigned long flags;
  719. struct _sas_port *mpt3sas_port, *next;
  720. struct _sas_node *sas_node;
  721. u8 found = 0;
  722. struct _sas_phy *mpt3sas_phy, *next_phy;
  723. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  724. sas_node = _transport_sas_node_find_by_sas_address(ioc,
  725. sas_address_parent);
  726. if (!sas_node) {
  727. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  728. return;
  729. }
  730. list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
  731. port_list) {
  732. if (mpt3sas_port->remote_identify.sas_address != sas_address)
  733. continue;
  734. found = 1;
  735. list_del(&mpt3sas_port->port_list);
  736. goto out;
  737. }
  738. out:
  739. if (!found) {
  740. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  741. return;
  742. }
  743. for (i = 0; i < sas_node->num_phys; i++) {
  744. if (sas_node->phy[i].remote_identify.sas_address == sas_address)
  745. memset(&sas_node->phy[i].remote_identify, 0 ,
  746. sizeof(struct sas_identify));
  747. }
  748. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  749. list_for_each_entry_safe(mpt3sas_phy, next_phy,
  750. &mpt3sas_port->phy_list, port_siblings) {
  751. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  752. dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
  753. "remove: sas_addr(0x%016llx), phy(%d)\n",
  754. (unsigned long long)
  755. mpt3sas_port->remote_identify.sas_address,
  756. mpt3sas_phy->phy_id);
  757. mpt3sas_phy->phy_belongs_to_port = 0;
  758. sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  759. list_del(&mpt3sas_phy->port_siblings);
  760. }
  761. sas_port_delete(mpt3sas_port->port);
  762. kfree(mpt3sas_port);
  763. }
  764. /**
  765. * mpt3sas_transport_add_host_phy - report sas_host phy to transport
  766. * @ioc: per adapter object
  767. * @mpt3sas_phy: mpt3sas per phy object
  768. * @phy_pg0: sas phy page 0
  769. * @parent_dev: parent device class object
  770. *
  771. * Returns 0 for success, non-zero for failure.
  772. */
  773. int
  774. mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
  775. *mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
  776. {
  777. struct sas_phy *phy;
  778. int phy_index = mpt3sas_phy->phy_id;
  779. INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
  780. phy = sas_phy_alloc(parent_dev, phy_index);
  781. if (!phy) {
  782. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  783. ioc->name, __FILE__, __LINE__, __func__);
  784. return -1;
  785. }
  786. if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
  787. &mpt3sas_phy->identify))) {
  788. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  789. ioc->name, __FILE__, __LINE__, __func__);
  790. sas_phy_free(phy);
  791. return -1;
  792. }
  793. phy->identify = mpt3sas_phy->identify;
  794. mpt3sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
  795. if (mpt3sas_phy->attached_handle)
  796. _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
  797. &mpt3sas_phy->remote_identify);
  798. phy->identify.phy_identifier = mpt3sas_phy->phy_id;
  799. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  800. phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  801. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  802. phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  803. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  804. phy_pg0.HwLinkRate >> 4);
  805. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  806. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  807. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  808. phy_pg0.ProgrammedLinkRate >> 4);
  809. if ((sas_phy_add(phy))) {
  810. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  811. ioc->name, __FILE__, __LINE__, __func__);
  812. sas_phy_free(phy);
  813. return -1;
  814. }
  815. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  816. dev_printk(KERN_INFO, &phy->dev,
  817. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  818. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  819. mpt3sas_phy->handle, (unsigned long long)
  820. mpt3sas_phy->identify.sas_address,
  821. mpt3sas_phy->attached_handle,
  822. (unsigned long long)
  823. mpt3sas_phy->remote_identify.sas_address);
  824. mpt3sas_phy->phy = phy;
  825. return 0;
  826. }
  827. /**
  828. * mpt3sas_transport_add_expander_phy - report expander phy to transport
  829. * @ioc: per adapter object
  830. * @mpt3sas_phy: mpt3sas per phy object
  831. * @expander_pg1: expander page 1
  832. * @parent_dev: parent device class object
  833. *
  834. * Returns 0 for success, non-zero for failure.
  835. */
  836. int
  837. mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
  838. *mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1,
  839. struct device *parent_dev)
  840. {
  841. struct sas_phy *phy;
  842. int phy_index = mpt3sas_phy->phy_id;
  843. INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
  844. phy = sas_phy_alloc(parent_dev, phy_index);
  845. if (!phy) {
  846. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  847. ioc->name, __FILE__, __LINE__, __func__);
  848. return -1;
  849. }
  850. if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
  851. &mpt3sas_phy->identify))) {
  852. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  853. ioc->name, __FILE__, __LINE__, __func__);
  854. sas_phy_free(phy);
  855. return -1;
  856. }
  857. phy->identify = mpt3sas_phy->identify;
  858. mpt3sas_phy->attached_handle =
  859. le16_to_cpu(expander_pg1.AttachedDevHandle);
  860. if (mpt3sas_phy->attached_handle)
  861. _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
  862. &mpt3sas_phy->remote_identify);
  863. phy->identify.phy_identifier = mpt3sas_phy->phy_id;
  864. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  865. expander_pg1.NegotiatedLinkRate &
  866. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  867. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  868. expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  869. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  870. expander_pg1.HwLinkRate >> 4);
  871. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  872. expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  873. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  874. expander_pg1.ProgrammedLinkRate >> 4);
  875. if ((sas_phy_add(phy))) {
  876. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  877. ioc->name, __FILE__, __LINE__, __func__);
  878. sas_phy_free(phy);
  879. return -1;
  880. }
  881. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  882. dev_printk(KERN_INFO, &phy->dev,
  883. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  884. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  885. mpt3sas_phy->handle, (unsigned long long)
  886. mpt3sas_phy->identify.sas_address,
  887. mpt3sas_phy->attached_handle,
  888. (unsigned long long)
  889. mpt3sas_phy->remote_identify.sas_address);
  890. mpt3sas_phy->phy = phy;
  891. return 0;
  892. }
  893. /**
  894. * mpt3sas_transport_update_links - refreshing phy link changes
  895. * @ioc: per adapter object
  896. * @sas_address: sas address of parent expander or sas host
  897. * @handle: attached device handle
  898. * @phy_numberv: phy number
  899. * @link_rate: new link rate
  900. *
  901. * Returns nothing.
  902. */
  903. void
  904. mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc,
  905. u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
  906. {
  907. unsigned long flags;
  908. struct _sas_node *sas_node;
  909. struct _sas_phy *mpt3sas_phy;
  910. if (ioc->shost_recovery || ioc->pci_error_recovery)
  911. return;
  912. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  913. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  914. if (!sas_node) {
  915. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  916. return;
  917. }
  918. mpt3sas_phy = &sas_node->phy[phy_number];
  919. mpt3sas_phy->attached_handle = handle;
  920. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  921. if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
  922. _transport_set_identify(ioc, handle,
  923. &mpt3sas_phy->remote_identify);
  924. _transport_add_phy_to_an_existing_port(ioc, sas_node,
  925. mpt3sas_phy, mpt3sas_phy->remote_identify.sas_address);
  926. } else
  927. memset(&mpt3sas_phy->remote_identify, 0 , sizeof(struct
  928. sas_identify));
  929. if (mpt3sas_phy->phy)
  930. mpt3sas_phy->phy->negotiated_linkrate =
  931. _transport_convert_phy_link_rate(link_rate);
  932. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  933. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  934. "refresh: parent sas_addr(0x%016llx),\n"
  935. "\tlink_rate(0x%02x), phy(%d)\n"
  936. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  937. (unsigned long long)sas_address,
  938. link_rate, phy_number, handle, (unsigned long long)
  939. mpt3sas_phy->remote_identify.sas_address);
  940. }
  941. static inline void *
  942. phy_to_ioc(struct sas_phy *phy)
  943. {
  944. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  945. return shost_priv(shost);
  946. }
  947. static inline void *
  948. rphy_to_ioc(struct sas_rphy *rphy)
  949. {
  950. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  951. return shost_priv(shost);
  952. }
  953. /* report phy error log structure */
  954. struct phy_error_log_request {
  955. u8 smp_frame_type; /* 0x40 */
  956. u8 function; /* 0x11 */
  957. u8 allocated_response_length;
  958. u8 request_length; /* 02 */
  959. u8 reserved_1[5];
  960. u8 phy_identifier;
  961. u8 reserved_2[2];
  962. };
  963. /* report phy error log reply structure */
  964. struct phy_error_log_reply {
  965. u8 smp_frame_type; /* 0x41 */
  966. u8 function; /* 0x11 */
  967. u8 function_result;
  968. u8 response_length;
  969. __be16 expander_change_count;
  970. u8 reserved_1[3];
  971. u8 phy_identifier;
  972. u8 reserved_2[2];
  973. __be32 invalid_dword;
  974. __be32 running_disparity_error;
  975. __be32 loss_of_dword_sync;
  976. __be32 phy_reset_problem;
  977. };
  978. /**
  979. * _transport_get_expander_phy_error_log - return expander counters
  980. * @ioc: per adapter object
  981. * @phy: The sas phy object
  982. *
  983. * Returns 0 for success, non-zero for failure.
  984. *
  985. */
  986. static int
  987. _transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc,
  988. struct sas_phy *phy)
  989. {
  990. Mpi2SmpPassthroughRequest_t *mpi_request;
  991. Mpi2SmpPassthroughReply_t *mpi_reply;
  992. struct phy_error_log_request *phy_error_log_request;
  993. struct phy_error_log_reply *phy_error_log_reply;
  994. int rc;
  995. u16 smid;
  996. u32 ioc_state;
  997. void *psge;
  998. u8 issue_reset = 0;
  999. void *data_out = NULL;
  1000. dma_addr_t data_out_dma;
  1001. u32 sz;
  1002. u16 wait_state_count;
  1003. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1004. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1005. __func__, ioc->name);
  1006. return -EFAULT;
  1007. }
  1008. mutex_lock(&ioc->transport_cmds.mutex);
  1009. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1010. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  1011. ioc->name, __func__);
  1012. rc = -EAGAIN;
  1013. goto out;
  1014. }
  1015. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1016. wait_state_count = 0;
  1017. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1018. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1019. if (wait_state_count++ == 10) {
  1020. pr_err(MPT3SAS_FMT
  1021. "%s: failed due to ioc not operational\n",
  1022. ioc->name, __func__);
  1023. rc = -EFAULT;
  1024. goto out;
  1025. }
  1026. ssleep(1);
  1027. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1028. pr_info(MPT3SAS_FMT
  1029. "%s: waiting for operational state(count=%d)\n",
  1030. ioc->name, __func__, wait_state_count);
  1031. }
  1032. if (wait_state_count)
  1033. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1034. ioc->name, __func__);
  1035. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1036. if (!smid) {
  1037. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1038. ioc->name, __func__);
  1039. rc = -EAGAIN;
  1040. goto out;
  1041. }
  1042. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1043. ioc->transport_cmds.smid = smid;
  1044. sz = sizeof(struct phy_error_log_request) +
  1045. sizeof(struct phy_error_log_reply);
  1046. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1047. if (!data_out) {
  1048. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  1049. __LINE__, __func__);
  1050. rc = -ENOMEM;
  1051. mpt3sas_base_free_smid(ioc, smid);
  1052. goto out;
  1053. }
  1054. rc = -EINVAL;
  1055. memset(data_out, 0, sz);
  1056. phy_error_log_request = data_out;
  1057. phy_error_log_request->smp_frame_type = 0x40;
  1058. phy_error_log_request->function = 0x11;
  1059. phy_error_log_request->request_length = 2;
  1060. phy_error_log_request->allocated_response_length = 0;
  1061. phy_error_log_request->phy_identifier = phy->number;
  1062. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1063. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1064. mpi_request->PhysicalPort = 0xFF;
  1065. mpi_request->VF_ID = 0; /* TODO */
  1066. mpi_request->VP_ID = 0;
  1067. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1068. mpi_request->RequestDataLength =
  1069. cpu_to_le16(sizeof(struct phy_error_log_request));
  1070. psge = &mpi_request->SGL;
  1071. ioc->build_sg(ioc, psge, data_out_dma,
  1072. sizeof(struct phy_error_log_request),
  1073. data_out_dma + sizeof(struct phy_error_log_request),
  1074. sizeof(struct phy_error_log_reply));
  1075. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1076. "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n",
  1077. ioc->name, (unsigned long long)phy->identify.sas_address,
  1078. phy->number));
  1079. init_completion(&ioc->transport_cmds.done);
  1080. mpt3sas_base_put_smid_default(ioc, smid);
  1081. wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
  1082. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1083. pr_err(MPT3SAS_FMT "%s: timeout\n",
  1084. ioc->name, __func__);
  1085. _debug_dump_mf(mpi_request,
  1086. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1087. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1088. issue_reset = 1;
  1089. goto issue_host_reset;
  1090. }
  1091. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1092. "phy_error_log - complete\n", ioc->name));
  1093. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1094. mpi_reply = ioc->transport_cmds.reply;
  1095. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1096. "phy_error_log - reply data transfer size(%d)\n",
  1097. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1098. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1099. sizeof(struct phy_error_log_reply))
  1100. goto out;
  1101. phy_error_log_reply = data_out +
  1102. sizeof(struct phy_error_log_request);
  1103. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1104. "phy_error_log - function_result(%d)\n",
  1105. ioc->name, phy_error_log_reply->function_result));
  1106. phy->invalid_dword_count =
  1107. be32_to_cpu(phy_error_log_reply->invalid_dword);
  1108. phy->running_disparity_error_count =
  1109. be32_to_cpu(phy_error_log_reply->running_disparity_error);
  1110. phy->loss_of_dword_sync_count =
  1111. be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
  1112. phy->phy_reset_problem_count =
  1113. be32_to_cpu(phy_error_log_reply->phy_reset_problem);
  1114. rc = 0;
  1115. } else
  1116. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1117. "phy_error_log - no reply\n", ioc->name));
  1118. issue_host_reset:
  1119. if (issue_reset)
  1120. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1121. out:
  1122. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1123. if (data_out)
  1124. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1125. mutex_unlock(&ioc->transport_cmds.mutex);
  1126. return rc;
  1127. }
  1128. /**
  1129. * _transport_get_linkerrors - return phy counters for both hba and expanders
  1130. * @phy: The sas phy object
  1131. *
  1132. * Returns 0 for success, non-zero for failure.
  1133. *
  1134. */
  1135. static int
  1136. _transport_get_linkerrors(struct sas_phy *phy)
  1137. {
  1138. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1139. unsigned long flags;
  1140. Mpi2ConfigReply_t mpi_reply;
  1141. Mpi2SasPhyPage1_t phy_pg1;
  1142. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1143. if (_transport_sas_node_find_by_sas_address(ioc,
  1144. phy->identify.sas_address) == NULL) {
  1145. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1146. return -EINVAL;
  1147. }
  1148. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1149. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1150. return _transport_get_expander_phy_error_log(ioc, phy);
  1151. /* get hba phy error logs */
  1152. if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
  1153. phy->number))) {
  1154. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1155. ioc->name, __FILE__, __LINE__, __func__);
  1156. return -ENXIO;
  1157. }
  1158. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1159. pr_info(MPT3SAS_FMT
  1160. "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n",
  1161. ioc->name, phy->number,
  1162. le16_to_cpu(mpi_reply.IOCStatus),
  1163. le32_to_cpu(mpi_reply.IOCLogInfo));
  1164. phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
  1165. phy->running_disparity_error_count =
  1166. le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
  1167. phy->loss_of_dword_sync_count =
  1168. le32_to_cpu(phy_pg1.LossDwordSynchCount);
  1169. phy->phy_reset_problem_count =
  1170. le32_to_cpu(phy_pg1.PhyResetProblemCount);
  1171. return 0;
  1172. }
  1173. /**
  1174. * _transport_get_enclosure_identifier -
  1175. * @phy: The sas phy object
  1176. *
  1177. * Obtain the enclosure logical id for an expander.
  1178. * Returns 0 for success, non-zero for failure.
  1179. */
  1180. static int
  1181. _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1182. {
  1183. struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1184. struct _sas_device *sas_device;
  1185. unsigned long flags;
  1186. int rc;
  1187. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1188. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  1189. rphy->identify.sas_address);
  1190. if (sas_device) {
  1191. *identifier = sas_device->enclosure_logical_id;
  1192. rc = 0;
  1193. sas_device_put(sas_device);
  1194. } else {
  1195. *identifier = 0;
  1196. rc = -ENXIO;
  1197. }
  1198. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1199. return rc;
  1200. }
  1201. /**
  1202. * _transport_get_bay_identifier -
  1203. * @phy: The sas phy object
  1204. *
  1205. * Returns the slot id for a device that resides inside an enclosure.
  1206. */
  1207. static int
  1208. _transport_get_bay_identifier(struct sas_rphy *rphy)
  1209. {
  1210. struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1211. struct _sas_device *sas_device;
  1212. unsigned long flags;
  1213. int rc;
  1214. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1215. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  1216. rphy->identify.sas_address);
  1217. if (sas_device) {
  1218. rc = sas_device->slot;
  1219. sas_device_put(sas_device);
  1220. } else {
  1221. rc = -ENXIO;
  1222. }
  1223. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1224. return rc;
  1225. }
  1226. /* phy control request structure */
  1227. struct phy_control_request {
  1228. u8 smp_frame_type; /* 0x40 */
  1229. u8 function; /* 0x91 */
  1230. u8 allocated_response_length;
  1231. u8 request_length; /* 0x09 */
  1232. u16 expander_change_count;
  1233. u8 reserved_1[3];
  1234. u8 phy_identifier;
  1235. u8 phy_operation;
  1236. u8 reserved_2[13];
  1237. u64 attached_device_name;
  1238. u8 programmed_min_physical_link_rate;
  1239. u8 programmed_max_physical_link_rate;
  1240. u8 reserved_3[6];
  1241. };
  1242. /* phy control reply structure */
  1243. struct phy_control_reply {
  1244. u8 smp_frame_type; /* 0x41 */
  1245. u8 function; /* 0x11 */
  1246. u8 function_result;
  1247. u8 response_length;
  1248. };
  1249. #define SMP_PHY_CONTROL_LINK_RESET (0x01)
  1250. #define SMP_PHY_CONTROL_HARD_RESET (0x02)
  1251. #define SMP_PHY_CONTROL_DISABLE (0x03)
  1252. /**
  1253. * _transport_expander_phy_control - expander phy control
  1254. * @ioc: per adapter object
  1255. * @phy: The sas phy object
  1256. *
  1257. * Returns 0 for success, non-zero for failure.
  1258. *
  1259. */
  1260. static int
  1261. _transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc,
  1262. struct sas_phy *phy, u8 phy_operation)
  1263. {
  1264. Mpi2SmpPassthroughRequest_t *mpi_request;
  1265. Mpi2SmpPassthroughReply_t *mpi_reply;
  1266. struct phy_control_request *phy_control_request;
  1267. struct phy_control_reply *phy_control_reply;
  1268. int rc;
  1269. u16 smid;
  1270. u32 ioc_state;
  1271. void *psge;
  1272. u8 issue_reset = 0;
  1273. void *data_out = NULL;
  1274. dma_addr_t data_out_dma;
  1275. u32 sz;
  1276. u16 wait_state_count;
  1277. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1278. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1279. __func__, ioc->name);
  1280. return -EFAULT;
  1281. }
  1282. mutex_lock(&ioc->transport_cmds.mutex);
  1283. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1284. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  1285. ioc->name, __func__);
  1286. rc = -EAGAIN;
  1287. goto out;
  1288. }
  1289. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1290. wait_state_count = 0;
  1291. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1292. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1293. if (wait_state_count++ == 10) {
  1294. pr_err(MPT3SAS_FMT
  1295. "%s: failed due to ioc not operational\n",
  1296. ioc->name, __func__);
  1297. rc = -EFAULT;
  1298. goto out;
  1299. }
  1300. ssleep(1);
  1301. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1302. pr_info(MPT3SAS_FMT
  1303. "%s: waiting for operational state(count=%d)\n",
  1304. ioc->name, __func__, wait_state_count);
  1305. }
  1306. if (wait_state_count)
  1307. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1308. ioc->name, __func__);
  1309. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1310. if (!smid) {
  1311. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1312. ioc->name, __func__);
  1313. rc = -EAGAIN;
  1314. goto out;
  1315. }
  1316. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1317. ioc->transport_cmds.smid = smid;
  1318. sz = sizeof(struct phy_control_request) +
  1319. sizeof(struct phy_control_reply);
  1320. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1321. if (!data_out) {
  1322. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  1323. __LINE__, __func__);
  1324. rc = -ENOMEM;
  1325. mpt3sas_base_free_smid(ioc, smid);
  1326. goto out;
  1327. }
  1328. rc = -EINVAL;
  1329. memset(data_out, 0, sz);
  1330. phy_control_request = data_out;
  1331. phy_control_request->smp_frame_type = 0x40;
  1332. phy_control_request->function = 0x91;
  1333. phy_control_request->request_length = 9;
  1334. phy_control_request->allocated_response_length = 0;
  1335. phy_control_request->phy_identifier = phy->number;
  1336. phy_control_request->phy_operation = phy_operation;
  1337. phy_control_request->programmed_min_physical_link_rate =
  1338. phy->minimum_linkrate << 4;
  1339. phy_control_request->programmed_max_physical_link_rate =
  1340. phy->maximum_linkrate << 4;
  1341. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1342. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1343. mpi_request->PhysicalPort = 0xFF;
  1344. mpi_request->VF_ID = 0; /* TODO */
  1345. mpi_request->VP_ID = 0;
  1346. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1347. mpi_request->RequestDataLength =
  1348. cpu_to_le16(sizeof(struct phy_error_log_request));
  1349. psge = &mpi_request->SGL;
  1350. ioc->build_sg(ioc, psge, data_out_dma,
  1351. sizeof(struct phy_control_request),
  1352. data_out_dma + sizeof(struct phy_control_request),
  1353. sizeof(struct phy_control_reply));
  1354. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1355. "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n",
  1356. ioc->name, (unsigned long long)phy->identify.sas_address,
  1357. phy->number, phy_operation));
  1358. init_completion(&ioc->transport_cmds.done);
  1359. mpt3sas_base_put_smid_default(ioc, smid);
  1360. wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
  1361. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1362. pr_err(MPT3SAS_FMT "%s: timeout\n",
  1363. ioc->name, __func__);
  1364. _debug_dump_mf(mpi_request,
  1365. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1366. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1367. issue_reset = 1;
  1368. goto issue_host_reset;
  1369. }
  1370. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1371. "phy_control - complete\n", ioc->name));
  1372. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1373. mpi_reply = ioc->transport_cmds.reply;
  1374. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1375. "phy_control - reply data transfer size(%d)\n",
  1376. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1377. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1378. sizeof(struct phy_control_reply))
  1379. goto out;
  1380. phy_control_reply = data_out +
  1381. sizeof(struct phy_control_request);
  1382. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1383. "phy_control - function_result(%d)\n",
  1384. ioc->name, phy_control_reply->function_result));
  1385. rc = 0;
  1386. } else
  1387. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1388. "phy_control - no reply\n", ioc->name));
  1389. issue_host_reset:
  1390. if (issue_reset)
  1391. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1392. out:
  1393. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1394. if (data_out)
  1395. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1396. mutex_unlock(&ioc->transport_cmds.mutex);
  1397. return rc;
  1398. }
  1399. /**
  1400. * _transport_phy_reset -
  1401. * @phy: The sas phy object
  1402. * @hard_reset:
  1403. *
  1404. * Returns 0 for success, non-zero for failure.
  1405. */
  1406. static int
  1407. _transport_phy_reset(struct sas_phy *phy, int hard_reset)
  1408. {
  1409. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1410. Mpi2SasIoUnitControlReply_t mpi_reply;
  1411. Mpi2SasIoUnitControlRequest_t mpi_request;
  1412. unsigned long flags;
  1413. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1414. if (_transport_sas_node_find_by_sas_address(ioc,
  1415. phy->identify.sas_address) == NULL) {
  1416. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1417. return -EINVAL;
  1418. }
  1419. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1420. /* handle expander phys */
  1421. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1422. return _transport_expander_phy_control(ioc, phy,
  1423. (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
  1424. SMP_PHY_CONTROL_LINK_RESET);
  1425. /* handle hba phys */
  1426. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  1427. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  1428. mpi_request.Operation = hard_reset ?
  1429. MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
  1430. mpi_request.PhyNum = phy->number;
  1431. if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
  1432. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1433. ioc->name, __FILE__, __LINE__, __func__);
  1434. return -ENXIO;
  1435. }
  1436. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1437. pr_info(MPT3SAS_FMT
  1438. "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  1439. ioc->name, phy->number, le16_to_cpu(mpi_reply.IOCStatus),
  1440. le32_to_cpu(mpi_reply.IOCLogInfo));
  1441. return 0;
  1442. }
  1443. /**
  1444. * _transport_phy_enable - enable/disable phys
  1445. * @phy: The sas phy object
  1446. * @enable: enable phy when true
  1447. *
  1448. * Only support sas_host direct attached phys.
  1449. * Returns 0 for success, non-zero for failure.
  1450. */
  1451. static int
  1452. _transport_phy_enable(struct sas_phy *phy, int enable)
  1453. {
  1454. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1455. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1456. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  1457. Mpi2ConfigReply_t mpi_reply;
  1458. u16 ioc_status;
  1459. u16 sz;
  1460. int rc = 0;
  1461. unsigned long flags;
  1462. int i, discovery_active;
  1463. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1464. if (_transport_sas_node_find_by_sas_address(ioc,
  1465. phy->identify.sas_address) == NULL) {
  1466. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1467. return -EINVAL;
  1468. }
  1469. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1470. /* handle expander phys */
  1471. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1472. return _transport_expander_phy_control(ioc, phy,
  1473. (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
  1474. SMP_PHY_CONTROL_DISABLE);
  1475. /* handle hba phys */
  1476. /* read sas_iounit page 0 */
  1477. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  1478. sizeof(Mpi2SasIOUnit0PhyData_t));
  1479. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  1480. if (!sas_iounit_pg0) {
  1481. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1482. ioc->name, __FILE__, __LINE__, __func__);
  1483. rc = -ENOMEM;
  1484. goto out;
  1485. }
  1486. if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  1487. sas_iounit_pg0, sz))) {
  1488. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1489. ioc->name, __FILE__, __LINE__, __func__);
  1490. rc = -ENXIO;
  1491. goto out;
  1492. }
  1493. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1494. MPI2_IOCSTATUS_MASK;
  1495. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1496. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1497. ioc->name, __FILE__, __LINE__, __func__);
  1498. rc = -EIO;
  1499. goto out;
  1500. }
  1501. /* unable to enable/disable phys when when discovery is active */
  1502. for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
  1503. if (sas_iounit_pg0->PhyData[i].PortFlags &
  1504. MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
  1505. pr_err(MPT3SAS_FMT "discovery is active on " \
  1506. "port = %d, phy = %d: unable to enable/disable "
  1507. "phys, try again later!\n", ioc->name,
  1508. sas_iounit_pg0->PhyData[i].Port, i);
  1509. discovery_active = 1;
  1510. }
  1511. }
  1512. if (discovery_active) {
  1513. rc = -EAGAIN;
  1514. goto out;
  1515. }
  1516. /* read sas_iounit page 1 */
  1517. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1518. sizeof(Mpi2SasIOUnit1PhyData_t));
  1519. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1520. if (!sas_iounit_pg1) {
  1521. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1522. ioc->name, __FILE__, __LINE__, __func__);
  1523. rc = -ENOMEM;
  1524. goto out;
  1525. }
  1526. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1527. sas_iounit_pg1, sz))) {
  1528. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1529. ioc->name, __FILE__, __LINE__, __func__);
  1530. rc = -ENXIO;
  1531. goto out;
  1532. }
  1533. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1534. MPI2_IOCSTATUS_MASK;
  1535. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1536. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1537. ioc->name, __FILE__, __LINE__, __func__);
  1538. rc = -EIO;
  1539. goto out;
  1540. }
  1541. /* copy Port/PortFlags/PhyFlags from page 0 */
  1542. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  1543. sas_iounit_pg1->PhyData[i].Port =
  1544. sas_iounit_pg0->PhyData[i].Port;
  1545. sas_iounit_pg1->PhyData[i].PortFlags =
  1546. (sas_iounit_pg0->PhyData[i].PortFlags &
  1547. MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
  1548. sas_iounit_pg1->PhyData[i].PhyFlags =
  1549. (sas_iounit_pg0->PhyData[i].PhyFlags &
  1550. (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
  1551. MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
  1552. }
  1553. if (enable)
  1554. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1555. &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1556. else
  1557. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1558. |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1559. mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
  1560. /* link reset */
  1561. if (enable)
  1562. _transport_phy_reset(phy, 0);
  1563. out:
  1564. kfree(sas_iounit_pg1);
  1565. kfree(sas_iounit_pg0);
  1566. return rc;
  1567. }
  1568. /**
  1569. * _transport_phy_speed - set phy min/max link rates
  1570. * @phy: The sas phy object
  1571. * @rates: rates defined in sas_phy_linkrates
  1572. *
  1573. * Only support sas_host direct attached phys.
  1574. * Returns 0 for success, non-zero for failure.
  1575. */
  1576. static int
  1577. _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  1578. {
  1579. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1580. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1581. Mpi2SasPhyPage0_t phy_pg0;
  1582. Mpi2ConfigReply_t mpi_reply;
  1583. u16 ioc_status;
  1584. u16 sz;
  1585. int i;
  1586. int rc = 0;
  1587. unsigned long flags;
  1588. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1589. if (_transport_sas_node_find_by_sas_address(ioc,
  1590. phy->identify.sas_address) == NULL) {
  1591. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1592. return -EINVAL;
  1593. }
  1594. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1595. if (!rates->minimum_linkrate)
  1596. rates->minimum_linkrate = phy->minimum_linkrate;
  1597. else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
  1598. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  1599. if (!rates->maximum_linkrate)
  1600. rates->maximum_linkrate = phy->maximum_linkrate;
  1601. else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
  1602. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  1603. /* handle expander phys */
  1604. if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
  1605. phy->minimum_linkrate = rates->minimum_linkrate;
  1606. phy->maximum_linkrate = rates->maximum_linkrate;
  1607. return _transport_expander_phy_control(ioc, phy,
  1608. SMP_PHY_CONTROL_LINK_RESET);
  1609. }
  1610. /* handle hba phys */
  1611. /* sas_iounit page 1 */
  1612. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1613. sizeof(Mpi2SasIOUnit1PhyData_t));
  1614. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1615. if (!sas_iounit_pg1) {
  1616. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1617. ioc->name, __FILE__, __LINE__, __func__);
  1618. rc = -ENOMEM;
  1619. goto out;
  1620. }
  1621. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1622. sas_iounit_pg1, sz))) {
  1623. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1624. ioc->name, __FILE__, __LINE__, __func__);
  1625. rc = -ENXIO;
  1626. goto out;
  1627. }
  1628. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1629. MPI2_IOCSTATUS_MASK;
  1630. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1631. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1632. ioc->name, __FILE__, __LINE__, __func__);
  1633. rc = -EIO;
  1634. goto out;
  1635. }
  1636. for (i = 0; i < ioc->sas_hba.num_phys; i++) {
  1637. if (phy->number != i) {
  1638. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1639. (ioc->sas_hba.phy[i].phy->minimum_linkrate +
  1640. (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
  1641. } else {
  1642. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1643. (rates->minimum_linkrate +
  1644. (rates->maximum_linkrate << 4));
  1645. }
  1646. }
  1647. if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  1648. sz)) {
  1649. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1650. ioc->name, __FILE__, __LINE__, __func__);
  1651. rc = -ENXIO;
  1652. goto out;
  1653. }
  1654. /* link reset */
  1655. _transport_phy_reset(phy, 0);
  1656. /* read phy page 0, then update the rates in the sas transport phy */
  1657. if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  1658. phy->number)) {
  1659. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  1660. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  1661. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  1662. phy_pg0.ProgrammedLinkRate >> 4);
  1663. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  1664. phy_pg0.NegotiatedLinkRate &
  1665. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  1666. }
  1667. out:
  1668. kfree(sas_iounit_pg1);
  1669. return rc;
  1670. }
  1671. /**
  1672. * _transport_smp_handler - transport portal for smp passthru
  1673. * @shost: shost object
  1674. * @rphy: sas transport rphy object
  1675. * @req:
  1676. *
  1677. * This used primarily for smp_utils.
  1678. * Example:
  1679. * smp_rep_general /sys/class/bsg/expander-5:0
  1680. */
  1681. static int
  1682. _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1683. struct request *req)
  1684. {
  1685. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1686. Mpi2SmpPassthroughRequest_t *mpi_request;
  1687. Mpi2SmpPassthroughReply_t *mpi_reply;
  1688. int rc;
  1689. u16 smid;
  1690. u32 ioc_state;
  1691. void *psge;
  1692. u8 issue_reset = 0;
  1693. dma_addr_t dma_addr_in = 0;
  1694. dma_addr_t dma_addr_out = 0;
  1695. dma_addr_t pci_dma_in = 0;
  1696. dma_addr_t pci_dma_out = 0;
  1697. void *pci_addr_in = NULL;
  1698. void *pci_addr_out = NULL;
  1699. u16 wait_state_count;
  1700. struct request *rsp = req->next_rq;
  1701. struct bio_vec bvec;
  1702. struct bvec_iter iter;
  1703. if (!rsp) {
  1704. pr_err(MPT3SAS_FMT "%s: the smp response space is missing\n",
  1705. ioc->name, __func__);
  1706. return -EINVAL;
  1707. }
  1708. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1709. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1710. __func__, ioc->name);
  1711. return -EFAULT;
  1712. }
  1713. rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
  1714. if (rc)
  1715. return rc;
  1716. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1717. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", ioc->name,
  1718. __func__);
  1719. rc = -EAGAIN;
  1720. goto out;
  1721. }
  1722. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1723. /* Check if the request is split across multiple segments */
  1724. if (bio_multiple_segments(req->bio)) {
  1725. u32 offset = 0;
  1726. /* Allocate memory and copy the request */
  1727. pci_addr_out = pci_alloc_consistent(ioc->pdev,
  1728. blk_rq_bytes(req), &pci_dma_out);
  1729. if (!pci_addr_out) {
  1730. pr_info(MPT3SAS_FMT "%s(): PCI Addr out = NULL\n",
  1731. ioc->name, __func__);
  1732. rc = -ENOMEM;
  1733. goto out;
  1734. }
  1735. bio_for_each_segment(bvec, req->bio, iter) {
  1736. memcpy(pci_addr_out + offset,
  1737. page_address(bvec.bv_page) + bvec.bv_offset,
  1738. bvec.bv_len);
  1739. offset += bvec.bv_len;
  1740. }
  1741. } else {
  1742. dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
  1743. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  1744. if (pci_dma_mapping_error(ioc->pdev, dma_addr_out)) {
  1745. pr_info(MPT3SAS_FMT "%s(): DMA Addr out = NULL\n",
  1746. ioc->name, __func__);
  1747. rc = -ENOMEM;
  1748. goto free_pci;
  1749. }
  1750. }
  1751. /* Check if the response needs to be populated across
  1752. * multiple segments */
  1753. if (bio_multiple_segments(rsp->bio)) {
  1754. pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
  1755. &pci_dma_in);
  1756. if (!pci_addr_in) {
  1757. pr_info(MPT3SAS_FMT "%s(): PCI Addr in = NULL\n",
  1758. ioc->name, __func__);
  1759. rc = -ENOMEM;
  1760. goto unmap;
  1761. }
  1762. } else {
  1763. dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
  1764. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  1765. if (pci_dma_mapping_error(ioc->pdev, dma_addr_in)) {
  1766. pr_info(MPT3SAS_FMT "%s(): DMA Addr in = NULL\n",
  1767. ioc->name, __func__);
  1768. rc = -ENOMEM;
  1769. goto unmap;
  1770. }
  1771. }
  1772. wait_state_count = 0;
  1773. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1774. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1775. if (wait_state_count++ == 10) {
  1776. pr_err(MPT3SAS_FMT
  1777. "%s: failed due to ioc not operational\n",
  1778. ioc->name, __func__);
  1779. rc = -EFAULT;
  1780. goto unmap;
  1781. }
  1782. ssleep(1);
  1783. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1784. pr_info(MPT3SAS_FMT
  1785. "%s: waiting for operational state(count=%d)\n",
  1786. ioc->name, __func__, wait_state_count);
  1787. }
  1788. if (wait_state_count)
  1789. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1790. ioc->name, __func__);
  1791. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1792. if (!smid) {
  1793. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1794. ioc->name, __func__);
  1795. rc = -EAGAIN;
  1796. goto unmap;
  1797. }
  1798. rc = 0;
  1799. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1800. ioc->transport_cmds.smid = smid;
  1801. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1802. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1803. mpi_request->PhysicalPort = 0xFF;
  1804. mpi_request->SASAddress = (rphy) ?
  1805. cpu_to_le64(rphy->identify.sas_address) :
  1806. cpu_to_le64(ioc->sas_hba.sas_address);
  1807. mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1808. psge = &mpi_request->SGL;
  1809. if (bio_multiple_segments(req->bio))
  1810. ioc->build_sg(ioc, psge, pci_dma_out, (blk_rq_bytes(req) - 4),
  1811. pci_dma_in, (blk_rq_bytes(rsp) + 4));
  1812. else
  1813. ioc->build_sg(ioc, psge, dma_addr_out, (blk_rq_bytes(req) - 4),
  1814. dma_addr_in, (blk_rq_bytes(rsp) + 4));
  1815. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1816. "%s - sending smp request\n", ioc->name, __func__));
  1817. init_completion(&ioc->transport_cmds.done);
  1818. mpt3sas_base_put_smid_default(ioc, smid);
  1819. wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
  1820. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1821. pr_err(MPT3SAS_FMT "%s : timeout\n",
  1822. __func__, ioc->name);
  1823. _debug_dump_mf(mpi_request,
  1824. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1825. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1826. issue_reset = 1;
  1827. goto issue_host_reset;
  1828. }
  1829. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1830. "%s - complete\n", ioc->name, __func__));
  1831. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1832. mpi_reply = ioc->transport_cmds.reply;
  1833. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1834. "%s - reply data transfer size(%d)\n",
  1835. ioc->name, __func__,
  1836. le16_to_cpu(mpi_reply->ResponseDataLength)));
  1837. memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
  1838. req->sense_len = sizeof(*mpi_reply);
  1839. req->resid_len = 0;
  1840. rsp->resid_len -=
  1841. le16_to_cpu(mpi_reply->ResponseDataLength);
  1842. /* check if the resp needs to be copied from the allocated
  1843. * pci mem */
  1844. if (bio_multiple_segments(rsp->bio)) {
  1845. u32 offset = 0;
  1846. u32 bytes_to_copy =
  1847. le16_to_cpu(mpi_reply->ResponseDataLength);
  1848. bio_for_each_segment(bvec, rsp->bio, iter) {
  1849. if (bytes_to_copy <= bvec.bv_len) {
  1850. memcpy(page_address(bvec.bv_page) +
  1851. bvec.bv_offset, pci_addr_in +
  1852. offset, bytes_to_copy);
  1853. break;
  1854. } else {
  1855. memcpy(page_address(bvec.bv_page) +
  1856. bvec.bv_offset, pci_addr_in +
  1857. offset, bvec.bv_len);
  1858. bytes_to_copy -= bvec.bv_len;
  1859. }
  1860. offset += bvec.bv_len;
  1861. }
  1862. }
  1863. } else {
  1864. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1865. "%s - no reply\n", ioc->name, __func__));
  1866. rc = -ENXIO;
  1867. }
  1868. issue_host_reset:
  1869. if (issue_reset) {
  1870. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1871. rc = -ETIMEDOUT;
  1872. }
  1873. unmap:
  1874. if (dma_addr_out)
  1875. pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
  1876. PCI_DMA_BIDIRECTIONAL);
  1877. if (dma_addr_in)
  1878. pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
  1879. PCI_DMA_BIDIRECTIONAL);
  1880. free_pci:
  1881. if (pci_addr_out)
  1882. pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
  1883. pci_dma_out);
  1884. if (pci_addr_in)
  1885. pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
  1886. pci_dma_in);
  1887. out:
  1888. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1889. mutex_unlock(&ioc->transport_cmds.mutex);
  1890. return rc;
  1891. }
  1892. struct sas_function_template mpt3sas_transport_functions = {
  1893. .get_linkerrors = _transport_get_linkerrors,
  1894. .get_enclosure_identifier = _transport_get_enclosure_identifier,
  1895. .get_bay_identifier = _transport_get_bay_identifier,
  1896. .phy_reset = _transport_phy_reset,
  1897. .phy_enable = _transport_phy_enable,
  1898. .set_phy_speed = _transport_phy_speed,
  1899. .smp_handler = _transport_smp_handler,
  1900. };
  1901. struct scsi_transport_template *mpt3sas_transport_template;