mpt2sas_transport.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  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-2013 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. _transport_del_phy_from_an_existing_port(ioc, sas_node,
  916. mpt2sas_phy);
  917. }
  918. if (mpt2sas_phy->phy)
  919. mpt2sas_phy->phy->negotiated_linkrate =
  920. _transport_convert_phy_link_rate(link_rate);
  921. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  922. dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
  923. "refresh: parent sas_addr(0x%016llx),\n"
  924. "\tlink_rate(0x%02x), phy(%d)\n"
  925. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  926. (unsigned long long)sas_address,
  927. link_rate, phy_number, handle, (unsigned long long)
  928. mpt2sas_phy->remote_identify.sas_address);
  929. }
  930. static inline void *
  931. phy_to_ioc(struct sas_phy *phy)
  932. {
  933. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  934. return shost_priv(shost);
  935. }
  936. static inline void *
  937. rphy_to_ioc(struct sas_rphy *rphy)
  938. {
  939. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  940. return shost_priv(shost);
  941. }
  942. /* report phy error log structure */
  943. struct phy_error_log_request{
  944. u8 smp_frame_type; /* 0x40 */
  945. u8 function; /* 0x11 */
  946. u8 allocated_response_length;
  947. u8 request_length; /* 02 */
  948. u8 reserved_1[5];
  949. u8 phy_identifier;
  950. u8 reserved_2[2];
  951. };
  952. /* report phy error log reply structure */
  953. struct phy_error_log_reply{
  954. u8 smp_frame_type; /* 0x41 */
  955. u8 function; /* 0x11 */
  956. u8 function_result;
  957. u8 response_length;
  958. __be16 expander_change_count;
  959. u8 reserved_1[3];
  960. u8 phy_identifier;
  961. u8 reserved_2[2];
  962. __be32 invalid_dword;
  963. __be32 running_disparity_error;
  964. __be32 loss_of_dword_sync;
  965. __be32 phy_reset_problem;
  966. };
  967. /**
  968. * _transport_get_expander_phy_error_log - return expander counters
  969. * @ioc: per adapter object
  970. * @phy: The sas phy object
  971. *
  972. * Returns 0 for success, non-zero for failure.
  973. *
  974. */
  975. static int
  976. _transport_get_expander_phy_error_log(struct MPT2SAS_ADAPTER *ioc,
  977. struct sas_phy *phy)
  978. {
  979. Mpi2SmpPassthroughRequest_t *mpi_request;
  980. Mpi2SmpPassthroughReply_t *mpi_reply;
  981. struct phy_error_log_request *phy_error_log_request;
  982. struct phy_error_log_reply *phy_error_log_reply;
  983. int rc;
  984. u16 smid;
  985. u32 ioc_state;
  986. unsigned long timeleft;
  987. void *psge;
  988. u32 sgl_flags;
  989. u8 issue_reset = 0;
  990. void *data_out = NULL;
  991. dma_addr_t data_out_dma;
  992. u32 sz;
  993. u16 wait_state_count;
  994. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  995. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  996. __func__, ioc->name);
  997. return -EFAULT;
  998. }
  999. mutex_lock(&ioc->transport_cmds.mutex);
  1000. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  1001. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
  1002. ioc->name, __func__);
  1003. rc = -EAGAIN;
  1004. goto out;
  1005. }
  1006. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  1007. wait_state_count = 0;
  1008. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1009. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1010. if (wait_state_count++ == 10) {
  1011. printk(MPT2SAS_ERR_FMT
  1012. "%s: failed due to ioc not operational\n",
  1013. ioc->name, __func__);
  1014. rc = -EFAULT;
  1015. goto out;
  1016. }
  1017. ssleep(1);
  1018. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1019. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  1020. "operational state(count=%d)\n", ioc->name,
  1021. __func__, wait_state_count);
  1022. }
  1023. if (wait_state_count)
  1024. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  1025. ioc->name, __func__);
  1026. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1027. if (!smid) {
  1028. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1029. ioc->name, __func__);
  1030. rc = -EAGAIN;
  1031. goto out;
  1032. }
  1033. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1034. ioc->transport_cmds.smid = smid;
  1035. sz = sizeof(struct phy_error_log_request) +
  1036. sizeof(struct phy_error_log_reply);
  1037. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1038. if (!data_out) {
  1039. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  1040. __LINE__, __func__);
  1041. rc = -ENOMEM;
  1042. mpt2sas_base_free_smid(ioc, smid);
  1043. goto out;
  1044. }
  1045. rc = -EINVAL;
  1046. memset(data_out, 0, sz);
  1047. phy_error_log_request = data_out;
  1048. phy_error_log_request->smp_frame_type = 0x40;
  1049. phy_error_log_request->function = 0x11;
  1050. phy_error_log_request->request_length = 2;
  1051. phy_error_log_request->allocated_response_length = 0;
  1052. phy_error_log_request->phy_identifier = phy->number;
  1053. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1054. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1055. mpi_request->PhysicalPort = 0xFF;
  1056. mpi_request->VF_ID = 0; /* TODO */
  1057. mpi_request->VP_ID = 0;
  1058. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1059. mpi_request->RequestDataLength =
  1060. cpu_to_le16(sizeof(struct phy_error_log_request));
  1061. psge = &mpi_request->SGL;
  1062. /* WRITE sgel first */
  1063. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1064. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1065. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1066. ioc->base_add_sg_single(psge, sgl_flags |
  1067. sizeof(struct phy_error_log_request), data_out_dma);
  1068. /* incr sgel */
  1069. psge += ioc->sge_size;
  1070. /* READ sgel last */
  1071. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1072. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1073. MPI2_SGE_FLAGS_END_OF_LIST);
  1074. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1075. ioc->base_add_sg_single(psge, sgl_flags |
  1076. sizeof(struct phy_error_log_reply), data_out_dma +
  1077. sizeof(struct phy_error_log_request));
  1078. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
  1079. "send to sas_addr(0x%016llx), phy(%d)\n", ioc->name,
  1080. (unsigned long long)phy->identify.sas_address, phy->number));
  1081. init_completion(&ioc->transport_cmds.done);
  1082. mpt2sas_base_put_smid_default(ioc, smid);
  1083. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1084. 10*HZ);
  1085. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  1086. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  1087. ioc->name, __func__);
  1088. _debug_dump_mf(mpi_request,
  1089. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1090. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  1091. issue_reset = 1;
  1092. goto issue_host_reset;
  1093. }
  1094. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
  1095. "complete\n", ioc->name));
  1096. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  1097. mpi_reply = ioc->transport_cmds.reply;
  1098. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1099. "phy_error_log - reply data transfer size(%d)\n",
  1100. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1101. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1102. sizeof(struct phy_error_log_reply))
  1103. goto out;
  1104. phy_error_log_reply = data_out +
  1105. sizeof(struct phy_error_log_request);
  1106. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1107. "phy_error_log - function_result(%d)\n",
  1108. ioc->name, phy_error_log_reply->function_result));
  1109. phy->invalid_dword_count =
  1110. be32_to_cpu(phy_error_log_reply->invalid_dword);
  1111. phy->running_disparity_error_count =
  1112. be32_to_cpu(phy_error_log_reply->running_disparity_error);
  1113. phy->loss_of_dword_sync_count =
  1114. be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
  1115. phy->phy_reset_problem_count =
  1116. be32_to_cpu(phy_error_log_reply->phy_reset_problem);
  1117. rc = 0;
  1118. } else
  1119. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1120. "phy_error_log - no reply\n", ioc->name));
  1121. issue_host_reset:
  1122. if (issue_reset)
  1123. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1124. FORCE_BIG_HAMMER);
  1125. out:
  1126. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  1127. if (data_out)
  1128. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1129. mutex_unlock(&ioc->transport_cmds.mutex);
  1130. return rc;
  1131. }
  1132. /**
  1133. * _transport_get_linkerrors - return phy counters for both hba and expanders
  1134. * @phy: The sas phy object
  1135. *
  1136. * Returns 0 for success, non-zero for failure.
  1137. *
  1138. */
  1139. static int
  1140. _transport_get_linkerrors(struct sas_phy *phy)
  1141. {
  1142. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1143. unsigned long flags;
  1144. Mpi2ConfigReply_t mpi_reply;
  1145. Mpi2SasPhyPage1_t phy_pg1;
  1146. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1147. if (_transport_sas_node_find_by_sas_address(ioc,
  1148. phy->identify.sas_address) == NULL) {
  1149. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1150. return -EINVAL;
  1151. }
  1152. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1153. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1154. return _transport_get_expander_phy_error_log(ioc, phy);
  1155. /* get hba phy error logs */
  1156. if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
  1157. phy->number))) {
  1158. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1159. ioc->name, __FILE__, __LINE__, __func__);
  1160. return -ENXIO;
  1161. }
  1162. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1163. printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
  1164. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  1165. phy->number, le16_to_cpu(mpi_reply.IOCStatus),
  1166. le32_to_cpu(mpi_reply.IOCLogInfo));
  1167. phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
  1168. phy->running_disparity_error_count =
  1169. le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
  1170. phy->loss_of_dword_sync_count =
  1171. le32_to_cpu(phy_pg1.LossDwordSynchCount);
  1172. phy->phy_reset_problem_count =
  1173. le32_to_cpu(phy_pg1.PhyResetProblemCount);
  1174. return 0;
  1175. }
  1176. /**
  1177. * _transport_get_enclosure_identifier -
  1178. * @phy: The sas phy object
  1179. *
  1180. * Obtain the enclosure logical id for an expander.
  1181. * Returns 0 for success, non-zero for failure.
  1182. */
  1183. static int
  1184. _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1185. {
  1186. struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1187. struct _sas_device *sas_device;
  1188. unsigned long flags;
  1189. int rc;
  1190. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1191. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1192. rphy->identify.sas_address);
  1193. if (sas_device) {
  1194. *identifier = sas_device->enclosure_logical_id;
  1195. rc = 0;
  1196. } else {
  1197. *identifier = 0;
  1198. rc = -ENXIO;
  1199. }
  1200. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1201. return rc;
  1202. }
  1203. /**
  1204. * _transport_get_bay_identifier -
  1205. * @phy: The sas phy object
  1206. *
  1207. * Returns the slot id for a device that resides inside an enclosure.
  1208. */
  1209. static int
  1210. _transport_get_bay_identifier(struct sas_rphy *rphy)
  1211. {
  1212. struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1213. struct _sas_device *sas_device;
  1214. unsigned long flags;
  1215. int rc;
  1216. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1217. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1218. rphy->identify.sas_address);
  1219. if (sas_device)
  1220. rc = sas_device->slot;
  1221. else
  1222. rc = -ENXIO;
  1223. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1224. return rc;
  1225. }
  1226. /* phy control request structure */
  1227. struct phy_control_request{
  1228. u8 smp_frame_type; /* 0x40 */
  1229. u8 function; /* 0x91 */
  1230. u8 allocated_response_length;
  1231. u8 request_length; /* 0x09 */
  1232. u16 expander_change_count;
  1233. u8 reserved_1[3];
  1234. u8 phy_identifier;
  1235. u8 phy_operation;
  1236. u8 reserved_2[13];
  1237. u64 attached_device_name;
  1238. u8 programmed_min_physical_link_rate;
  1239. u8 programmed_max_physical_link_rate;
  1240. u8 reserved_3[6];
  1241. };
  1242. /* phy control reply structure */
  1243. struct phy_control_reply{
  1244. u8 smp_frame_type; /* 0x41 */
  1245. u8 function; /* 0x11 */
  1246. u8 function_result;
  1247. u8 response_length;
  1248. };
  1249. #define SMP_PHY_CONTROL_LINK_RESET (0x01)
  1250. #define SMP_PHY_CONTROL_HARD_RESET (0x02)
  1251. #define SMP_PHY_CONTROL_DISABLE (0x03)
  1252. /**
  1253. * _transport_expander_phy_control - expander phy control
  1254. * @ioc: per adapter object
  1255. * @phy: The sas phy object
  1256. *
  1257. * Returns 0 for success, non-zero for failure.
  1258. *
  1259. */
  1260. static int
  1261. _transport_expander_phy_control(struct MPT2SAS_ADAPTER *ioc,
  1262. struct sas_phy *phy, u8 phy_operation)
  1263. {
  1264. Mpi2SmpPassthroughRequest_t *mpi_request;
  1265. Mpi2SmpPassthroughReply_t *mpi_reply;
  1266. struct phy_control_request *phy_control_request;
  1267. struct phy_control_reply *phy_control_reply;
  1268. int rc;
  1269. u16 smid;
  1270. u32 ioc_state;
  1271. unsigned long timeleft;
  1272. void *psge;
  1273. u32 sgl_flags;
  1274. u8 issue_reset = 0;
  1275. void *data_out = NULL;
  1276. dma_addr_t data_out_dma;
  1277. u32 sz;
  1278. u16 wait_state_count;
  1279. if (ioc->shost_recovery) {
  1280. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1281. __func__, ioc->name);
  1282. return -EFAULT;
  1283. }
  1284. mutex_lock(&ioc->transport_cmds.mutex);
  1285. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  1286. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
  1287. ioc->name, __func__);
  1288. rc = -EAGAIN;
  1289. goto out;
  1290. }
  1291. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  1292. wait_state_count = 0;
  1293. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1294. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1295. if (wait_state_count++ == 10) {
  1296. printk(MPT2SAS_ERR_FMT
  1297. "%s: failed due to ioc not operational\n",
  1298. ioc->name, __func__);
  1299. rc = -EFAULT;
  1300. goto out;
  1301. }
  1302. ssleep(1);
  1303. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1304. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  1305. "operational state(count=%d)\n", ioc->name,
  1306. __func__, wait_state_count);
  1307. }
  1308. if (wait_state_count)
  1309. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  1310. ioc->name, __func__);
  1311. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1312. if (!smid) {
  1313. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1314. ioc->name, __func__);
  1315. rc = -EAGAIN;
  1316. goto out;
  1317. }
  1318. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1319. ioc->transport_cmds.smid = smid;
  1320. sz = sizeof(struct phy_control_request) +
  1321. sizeof(struct phy_control_reply);
  1322. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1323. if (!data_out) {
  1324. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  1325. __LINE__, __func__);
  1326. rc = -ENOMEM;
  1327. mpt2sas_base_free_smid(ioc, smid);
  1328. goto out;
  1329. }
  1330. rc = -EINVAL;
  1331. memset(data_out, 0, sz);
  1332. phy_control_request = data_out;
  1333. phy_control_request->smp_frame_type = 0x40;
  1334. phy_control_request->function = 0x91;
  1335. phy_control_request->request_length = 9;
  1336. phy_control_request->allocated_response_length = 0;
  1337. phy_control_request->phy_identifier = phy->number;
  1338. phy_control_request->phy_operation = phy_operation;
  1339. phy_control_request->programmed_min_physical_link_rate =
  1340. phy->minimum_linkrate << 4;
  1341. phy_control_request->programmed_max_physical_link_rate =
  1342. phy->maximum_linkrate << 4;
  1343. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1344. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1345. mpi_request->PhysicalPort = 0xFF;
  1346. mpi_request->VF_ID = 0; /* TODO */
  1347. mpi_request->VP_ID = 0;
  1348. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1349. mpi_request->RequestDataLength =
  1350. cpu_to_le16(sizeof(struct phy_error_log_request));
  1351. psge = &mpi_request->SGL;
  1352. /* WRITE sgel first */
  1353. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1354. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1355. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1356. ioc->base_add_sg_single(psge, sgl_flags |
  1357. sizeof(struct phy_control_request), data_out_dma);
  1358. /* incr sgel */
  1359. psge += ioc->sge_size;
  1360. /* READ sgel last */
  1361. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1362. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1363. MPI2_SGE_FLAGS_END_OF_LIST);
  1364. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1365. ioc->base_add_sg_single(psge, sgl_flags |
  1366. sizeof(struct phy_control_reply), data_out_dma +
  1367. sizeof(struct phy_control_request));
  1368. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
  1369. "send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n", ioc->name,
  1370. (unsigned long long)phy->identify.sas_address, phy->number,
  1371. phy_operation));
  1372. init_completion(&ioc->transport_cmds.done);
  1373. mpt2sas_base_put_smid_default(ioc, smid);
  1374. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1375. 10*HZ);
  1376. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  1377. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  1378. ioc->name, __func__);
  1379. _debug_dump_mf(mpi_request,
  1380. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1381. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  1382. issue_reset = 1;
  1383. goto issue_host_reset;
  1384. }
  1385. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
  1386. "complete\n", ioc->name));
  1387. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  1388. mpi_reply = ioc->transport_cmds.reply;
  1389. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1390. "phy_control - reply data transfer size(%d)\n",
  1391. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1392. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1393. sizeof(struct phy_control_reply))
  1394. goto out;
  1395. phy_control_reply = data_out +
  1396. sizeof(struct phy_control_request);
  1397. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1398. "phy_control - function_result(%d)\n",
  1399. ioc->name, phy_control_reply->function_result));
  1400. rc = 0;
  1401. } else
  1402. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1403. "phy_control - no reply\n", ioc->name));
  1404. issue_host_reset:
  1405. if (issue_reset)
  1406. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1407. FORCE_BIG_HAMMER);
  1408. out:
  1409. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  1410. if (data_out)
  1411. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1412. mutex_unlock(&ioc->transport_cmds.mutex);
  1413. return rc;
  1414. }
  1415. /**
  1416. * _transport_phy_reset -
  1417. * @phy: The sas phy object
  1418. * @hard_reset:
  1419. *
  1420. * Returns 0 for success, non-zero for failure.
  1421. */
  1422. static int
  1423. _transport_phy_reset(struct sas_phy *phy, int hard_reset)
  1424. {
  1425. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1426. Mpi2SasIoUnitControlReply_t mpi_reply;
  1427. Mpi2SasIoUnitControlRequest_t mpi_request;
  1428. unsigned long flags;
  1429. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1430. if (_transport_sas_node_find_by_sas_address(ioc,
  1431. phy->identify.sas_address) == NULL) {
  1432. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1433. return -EINVAL;
  1434. }
  1435. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1436. /* handle expander phys */
  1437. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1438. return _transport_expander_phy_control(ioc, phy,
  1439. (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
  1440. SMP_PHY_CONTROL_LINK_RESET);
  1441. /* handle hba phys */
  1442. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  1443. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  1444. mpi_request.Operation = hard_reset ?
  1445. MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
  1446. mpi_request.PhyNum = phy->number;
  1447. if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
  1448. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1449. ioc->name, __FILE__, __LINE__, __func__);
  1450. return -ENXIO;
  1451. }
  1452. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1453. printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
  1454. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  1455. phy->number, le16_to_cpu(mpi_reply.IOCStatus),
  1456. le32_to_cpu(mpi_reply.IOCLogInfo));
  1457. return 0;
  1458. }
  1459. /**
  1460. * _transport_phy_enable - enable/disable phys
  1461. * @phy: The sas phy object
  1462. * @enable: enable phy when true
  1463. *
  1464. * Only support sas_host direct attached phys.
  1465. * Returns 0 for success, non-zero for failure.
  1466. */
  1467. static int
  1468. _transport_phy_enable(struct sas_phy *phy, int enable)
  1469. {
  1470. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1471. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1472. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  1473. Mpi2ConfigReply_t mpi_reply;
  1474. u16 ioc_status;
  1475. u16 sz;
  1476. int rc = 0;
  1477. unsigned long flags;
  1478. int i, discovery_active;
  1479. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1480. if (_transport_sas_node_find_by_sas_address(ioc,
  1481. phy->identify.sas_address) == NULL) {
  1482. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1483. return -EINVAL;
  1484. }
  1485. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1486. /* handle expander phys */
  1487. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1488. return _transport_expander_phy_control(ioc, phy,
  1489. (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
  1490. SMP_PHY_CONTROL_DISABLE);
  1491. /* handle hba phys */
  1492. /* read sas_iounit page 0 */
  1493. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  1494. sizeof(Mpi2SasIOUnit0PhyData_t));
  1495. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  1496. if (!sas_iounit_pg0) {
  1497. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1498. ioc->name, __FILE__, __LINE__, __func__);
  1499. rc = -ENOMEM;
  1500. goto out;
  1501. }
  1502. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  1503. sas_iounit_pg0, sz))) {
  1504. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1505. ioc->name, __FILE__, __LINE__, __func__);
  1506. rc = -ENXIO;
  1507. goto out;
  1508. }
  1509. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1510. MPI2_IOCSTATUS_MASK;
  1511. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1512. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1513. ioc->name, __FILE__, __LINE__, __func__);
  1514. rc = -EIO;
  1515. goto out;
  1516. }
  1517. /* unable to enable/disable phys when when discovery is active */
  1518. for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
  1519. if (sas_iounit_pg0->PhyData[i].PortFlags &
  1520. MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
  1521. printk(MPT2SAS_ERR_FMT "discovery is active on "
  1522. "port = %d, phy = %d: unable to enable/disable "
  1523. "phys, try again later!\n", ioc->name,
  1524. sas_iounit_pg0->PhyData[i].Port, i);
  1525. discovery_active = 1;
  1526. }
  1527. }
  1528. if (discovery_active) {
  1529. rc = -EAGAIN;
  1530. goto out;
  1531. }
  1532. /* read sas_iounit page 1 */
  1533. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1534. sizeof(Mpi2SasIOUnit1PhyData_t));
  1535. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1536. if (!sas_iounit_pg1) {
  1537. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1538. ioc->name, __FILE__, __LINE__, __func__);
  1539. rc = -ENOMEM;
  1540. goto out;
  1541. }
  1542. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1543. sas_iounit_pg1, sz))) {
  1544. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1545. ioc->name, __FILE__, __LINE__, __func__);
  1546. rc = -ENXIO;
  1547. goto out;
  1548. }
  1549. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1550. MPI2_IOCSTATUS_MASK;
  1551. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1552. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1553. ioc->name, __FILE__, __LINE__, __func__);
  1554. rc = -EIO;
  1555. goto out;
  1556. }
  1557. /* copy Port/PortFlags/PhyFlags from page 0 */
  1558. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  1559. sas_iounit_pg1->PhyData[i].Port =
  1560. sas_iounit_pg0->PhyData[i].Port;
  1561. sas_iounit_pg1->PhyData[i].PortFlags =
  1562. (sas_iounit_pg0->PhyData[i].PortFlags &
  1563. MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
  1564. sas_iounit_pg1->PhyData[i].PhyFlags =
  1565. (sas_iounit_pg0->PhyData[i].PhyFlags &
  1566. (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
  1567. MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
  1568. }
  1569. if (enable)
  1570. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1571. &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1572. else
  1573. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1574. |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1575. mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
  1576. /* link reset */
  1577. if (enable)
  1578. _transport_phy_reset(phy, 0);
  1579. out:
  1580. kfree(sas_iounit_pg1);
  1581. kfree(sas_iounit_pg0);
  1582. return rc;
  1583. }
  1584. /**
  1585. * _transport_phy_speed - set phy min/max link rates
  1586. * @phy: The sas phy object
  1587. * @rates: rates defined in sas_phy_linkrates
  1588. *
  1589. * Only support sas_host direct attached phys.
  1590. * Returns 0 for success, non-zero for failure.
  1591. */
  1592. static int
  1593. _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  1594. {
  1595. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1596. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1597. Mpi2SasPhyPage0_t phy_pg0;
  1598. Mpi2ConfigReply_t mpi_reply;
  1599. u16 ioc_status;
  1600. u16 sz;
  1601. int i;
  1602. int rc = 0;
  1603. unsigned long flags;
  1604. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1605. if (_transport_sas_node_find_by_sas_address(ioc,
  1606. phy->identify.sas_address) == NULL) {
  1607. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1608. return -EINVAL;
  1609. }
  1610. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1611. if (!rates->minimum_linkrate)
  1612. rates->minimum_linkrate = phy->minimum_linkrate;
  1613. else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
  1614. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  1615. if (!rates->maximum_linkrate)
  1616. rates->maximum_linkrate = phy->maximum_linkrate;
  1617. else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
  1618. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  1619. /* handle expander phys */
  1620. if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
  1621. phy->minimum_linkrate = rates->minimum_linkrate;
  1622. phy->maximum_linkrate = rates->maximum_linkrate;
  1623. return _transport_expander_phy_control(ioc, phy,
  1624. SMP_PHY_CONTROL_LINK_RESET);
  1625. }
  1626. /* handle hba phys */
  1627. /* sas_iounit page 1 */
  1628. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1629. sizeof(Mpi2SasIOUnit1PhyData_t));
  1630. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1631. if (!sas_iounit_pg1) {
  1632. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1633. ioc->name, __FILE__, __LINE__, __func__);
  1634. rc = -ENOMEM;
  1635. goto out;
  1636. }
  1637. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1638. sas_iounit_pg1, sz))) {
  1639. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1640. ioc->name, __FILE__, __LINE__, __func__);
  1641. rc = -ENXIO;
  1642. goto out;
  1643. }
  1644. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1645. MPI2_IOCSTATUS_MASK;
  1646. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1647. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1648. ioc->name, __FILE__, __LINE__, __func__);
  1649. rc = -EIO;
  1650. goto out;
  1651. }
  1652. for (i = 0; i < ioc->sas_hba.num_phys; i++) {
  1653. if (phy->number != i) {
  1654. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1655. (ioc->sas_hba.phy[i].phy->minimum_linkrate +
  1656. (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
  1657. } else {
  1658. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1659. (rates->minimum_linkrate +
  1660. (rates->maximum_linkrate << 4));
  1661. }
  1662. }
  1663. if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  1664. sz)) {
  1665. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1666. ioc->name, __FILE__, __LINE__, __func__);
  1667. rc = -ENXIO;
  1668. goto out;
  1669. }
  1670. /* link reset */
  1671. _transport_phy_reset(phy, 0);
  1672. /* read phy page 0, then update the rates in the sas transport phy */
  1673. if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  1674. phy->number)) {
  1675. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  1676. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  1677. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  1678. phy_pg0.ProgrammedLinkRate >> 4);
  1679. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  1680. phy_pg0.NegotiatedLinkRate &
  1681. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  1682. }
  1683. out:
  1684. kfree(sas_iounit_pg1);
  1685. return rc;
  1686. }
  1687. /**
  1688. * _transport_smp_handler - transport portal for smp passthru
  1689. * @shost: shost object
  1690. * @rphy: sas transport rphy object
  1691. * @req:
  1692. *
  1693. * This used primarily for smp_utils.
  1694. * Example:
  1695. * smp_rep_general /sys/class/bsg/expander-5:0
  1696. */
  1697. static int
  1698. _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1699. struct request *req)
  1700. {
  1701. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1702. Mpi2SmpPassthroughRequest_t *mpi_request;
  1703. Mpi2SmpPassthroughReply_t *mpi_reply;
  1704. int rc, i;
  1705. u16 smid;
  1706. u32 ioc_state;
  1707. unsigned long timeleft;
  1708. void *psge;
  1709. u32 sgl_flags;
  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 = NULL;
  1720. if (!rsp) {
  1721. printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
  1722. "missing\n", ioc->name, __func__);
  1723. return -EINVAL;
  1724. }
  1725. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1726. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1727. __func__, ioc->name);
  1728. return -EFAULT;
  1729. }
  1730. rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
  1731. if (rc)
  1732. return rc;
  1733. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  1734. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
  1735. __func__);
  1736. rc = -EAGAIN;
  1737. goto out;
  1738. }
  1739. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  1740. /* Check if the request is split across multiple segments */
  1741. if (bio_segments(req->bio) > 1) {
  1742. u32 offset = 0;
  1743. /* Allocate memory and copy the request */
  1744. pci_addr_out = pci_alloc_consistent(ioc->pdev,
  1745. blk_rq_bytes(req), &pci_dma_out);
  1746. if (!pci_addr_out) {
  1747. printk(MPT2SAS_INFO_FMT "%s(): PCI Addr out = NULL\n",
  1748. ioc->name, __func__);
  1749. rc = -ENOMEM;
  1750. goto out;
  1751. }
  1752. bio_for_each_segment(bvec, req->bio, i) {
  1753. memcpy(pci_addr_out + offset,
  1754. page_address(bvec->bv_page) + bvec->bv_offset,
  1755. bvec->bv_len);
  1756. offset += bvec->bv_len;
  1757. }
  1758. } else {
  1759. dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
  1760. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  1761. if (!dma_addr_out) {
  1762. printk(MPT2SAS_INFO_FMT "%s(): DMA Addr out = NULL\n",
  1763. ioc->name, __func__);
  1764. rc = -ENOMEM;
  1765. goto free_pci;
  1766. }
  1767. }
  1768. /* Check if the response needs to be populated across
  1769. * multiple segments */
  1770. if (bio_segments(rsp->bio) > 1) {
  1771. pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
  1772. &pci_dma_in);
  1773. if (!pci_addr_in) {
  1774. printk(MPT2SAS_INFO_FMT "%s(): PCI Addr in = NULL\n",
  1775. ioc->name, __func__);
  1776. rc = -ENOMEM;
  1777. goto unmap;
  1778. }
  1779. } else {
  1780. dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
  1781. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  1782. if (!dma_addr_in) {
  1783. printk(MPT2SAS_INFO_FMT "%s(): DMA Addr in = NULL\n",
  1784. ioc->name, __func__);
  1785. rc = -ENOMEM;
  1786. goto unmap;
  1787. }
  1788. }
  1789. wait_state_count = 0;
  1790. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1791. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1792. if (wait_state_count++ == 10) {
  1793. printk(MPT2SAS_ERR_FMT
  1794. "%s: failed due to ioc not operational\n",
  1795. ioc->name, __func__);
  1796. rc = -EFAULT;
  1797. goto unmap;
  1798. }
  1799. ssleep(1);
  1800. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1801. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  1802. "operational state(count=%d)\n", ioc->name,
  1803. __func__, wait_state_count);
  1804. }
  1805. if (wait_state_count)
  1806. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  1807. ioc->name, __func__);
  1808. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1809. if (!smid) {
  1810. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1811. ioc->name, __func__);
  1812. rc = -EAGAIN;
  1813. goto unmap;
  1814. }
  1815. rc = 0;
  1816. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1817. ioc->transport_cmds.smid = smid;
  1818. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1819. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1820. mpi_request->PhysicalPort = 0xFF;
  1821. mpi_request->VF_ID = 0; /* TODO */
  1822. mpi_request->VP_ID = 0;
  1823. mpi_request->SASAddress = (rphy) ?
  1824. cpu_to_le64(rphy->identify.sas_address) :
  1825. cpu_to_le64(ioc->sas_hba.sas_address);
  1826. mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1827. psge = &mpi_request->SGL;
  1828. /* WRITE sgel first */
  1829. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1830. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1831. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1832. if (bio_segments(req->bio) > 1) {
  1833. ioc->base_add_sg_single(psge, sgl_flags |
  1834. (blk_rq_bytes(req) - 4), pci_dma_out);
  1835. } else {
  1836. ioc->base_add_sg_single(psge, sgl_flags |
  1837. (blk_rq_bytes(req) - 4), dma_addr_out);
  1838. }
  1839. /* incr sgel */
  1840. psge += ioc->sge_size;
  1841. /* READ sgel last */
  1842. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1843. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1844. MPI2_SGE_FLAGS_END_OF_LIST);
  1845. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1846. if (bio_segments(rsp->bio) > 1) {
  1847. ioc->base_add_sg_single(psge, sgl_flags |
  1848. (blk_rq_bytes(rsp) + 4), pci_dma_in);
  1849. } else {
  1850. ioc->base_add_sg_single(psge, sgl_flags |
  1851. (blk_rq_bytes(rsp) + 4), dma_addr_in);
  1852. }
  1853. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
  1854. "sending smp request\n", ioc->name, __func__));
  1855. init_completion(&ioc->transport_cmds.done);
  1856. mpt2sas_base_put_smid_default(ioc, smid);
  1857. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1858. 10*HZ);
  1859. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  1860. printk(MPT2SAS_ERR_FMT "%s : timeout\n",
  1861. __func__, ioc->name);
  1862. _debug_dump_mf(mpi_request,
  1863. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1864. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  1865. issue_reset = 1;
  1866. goto issue_host_reset;
  1867. }
  1868. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
  1869. "complete\n", ioc->name, __func__));
  1870. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  1871. mpi_reply = ioc->transport_cmds.reply;
  1872. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1873. "%s - reply data transfer size(%d)\n",
  1874. ioc->name, __func__,
  1875. le16_to_cpu(mpi_reply->ResponseDataLength)));
  1876. memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
  1877. req->sense_len = sizeof(*mpi_reply);
  1878. req->resid_len = 0;
  1879. rsp->resid_len -=
  1880. le16_to_cpu(mpi_reply->ResponseDataLength);
  1881. /* check if the resp needs to be copied from the allocated
  1882. * pci mem */
  1883. if (bio_segments(rsp->bio) > 1) {
  1884. u32 offset = 0;
  1885. u32 bytes_to_copy =
  1886. le16_to_cpu(mpi_reply->ResponseDataLength);
  1887. bio_for_each_segment(bvec, rsp->bio, i) {
  1888. if (bytes_to_copy <= bvec->bv_len) {
  1889. memcpy(page_address(bvec->bv_page) +
  1890. bvec->bv_offset, pci_addr_in +
  1891. offset, bytes_to_copy);
  1892. break;
  1893. } else {
  1894. memcpy(page_address(bvec->bv_page) +
  1895. bvec->bv_offset, pci_addr_in +
  1896. offset, bvec->bv_len);
  1897. bytes_to_copy -= bvec->bv_len;
  1898. }
  1899. offset += bvec->bv_len;
  1900. }
  1901. }
  1902. } else {
  1903. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1904. "%s - no reply\n", ioc->name, __func__));
  1905. rc = -ENXIO;
  1906. }
  1907. issue_host_reset:
  1908. if (issue_reset) {
  1909. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1910. FORCE_BIG_HAMMER);
  1911. rc = -ETIMEDOUT;
  1912. }
  1913. unmap:
  1914. if (dma_addr_out)
  1915. pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
  1916. PCI_DMA_BIDIRECTIONAL);
  1917. if (dma_addr_in)
  1918. pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
  1919. PCI_DMA_BIDIRECTIONAL);
  1920. free_pci:
  1921. if (pci_addr_out)
  1922. pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
  1923. pci_dma_out);
  1924. if (pci_addr_in)
  1925. pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
  1926. pci_dma_in);
  1927. out:
  1928. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  1929. mutex_unlock(&ioc->transport_cmds.mutex);
  1930. return rc;
  1931. }
  1932. struct sas_function_template mpt2sas_transport_functions = {
  1933. .get_linkerrors = _transport_get_linkerrors,
  1934. .get_enclosure_identifier = _transport_get_enclosure_identifier,
  1935. .get_bay_identifier = _transport_get_bay_identifier,
  1936. .phy_reset = _transport_phy_reset,
  1937. .phy_enable = _transport_phy_enable,
  1938. .set_phy_speed = _transport_phy_speed,
  1939. .smp_handler = _transport_smp_handler,
  1940. };
  1941. struct scsi_transport_template *mpt2sas_transport_template;