mpt3sas_transport.c 63 KB

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