mpt2sas_transport.c 64 KB

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