ibmvscsi.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. /* ------------------------------------------------------------
  2. * ibmvscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2004
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.com)
  6. * Dave Boutcher (sleddog@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. *
  23. * ------------------------------------------------------------
  24. * Emulation of a SCSI host adapter for Virtual I/O devices
  25. *
  26. * This driver supports the SCSI adapter implemented by the IBM
  27. * Power5 firmware. That SCSI adapter is not a physical adapter,
  28. * but allows Linux SCSI peripheral drivers to directly
  29. * access devices in another logical partition on the physical system.
  30. *
  31. * The virtual adapter(s) are present in the open firmware device
  32. * tree just like real adapters.
  33. *
  34. * One of the capabilities provided on these systems is the ability
  35. * to DMA between partitions. The architecture states that for VSCSI,
  36. * the server side is allowed to DMA to and from the client. The client
  37. * is never trusted to DMA to or from the server directly.
  38. *
  39. * Messages are sent between partitions on a "Command/Response Queue"
  40. * (CRQ), which is just a buffer of 16 byte entries in the receiver's
  41. * Senders cannot access the buffer directly, but send messages by
  42. * making a hypervisor call and passing in the 16 bytes. The hypervisor
  43. * puts the message in the next 16 byte space in round-robin fashion,
  44. * turns on the high order bit of the message (the valid bit), and
  45. * generates an interrupt to the receiver (if interrupts are turned on.)
  46. * The receiver just turns off the valid bit when they have copied out
  47. * the message.
  48. *
  49. * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
  50. * (IU) (as defined in the T10 standard available at www.t10.org), gets
  51. * a DMA address for the message, and sends it to the server as the
  52. * payload of a CRQ message. The server DMAs the SRP IU and processes it,
  53. * including doing any additional data transfers. When it is done, it
  54. * DMAs the SRP response back to the same address as the request came from,
  55. * and sends a CRQ message back to inform the client that the request has
  56. * completed.
  57. *
  58. * Note that some of the underlying infrastructure is different between
  59. * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
  60. * the older iSeries hypervisor models. To support both, some low level
  61. * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
  62. * The Makefile should pick one, not two, not zero, of these.
  63. *
  64. * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
  65. * interfaces. It would be really nice to abstract this above an RDMA
  66. * layer.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/dma-mapping.h>
  71. #include <linux/delay.h>
  72. #include <linux/slab.h>
  73. #include <linux/of.h>
  74. #include <linux/pm.h>
  75. #include <linux/kthread.h>
  76. #include <asm/firmware.h>
  77. #include <asm/vio.h>
  78. #include <scsi/scsi.h>
  79. #include <scsi/scsi_cmnd.h>
  80. #include <scsi/scsi_host.h>
  81. #include <scsi/scsi_device.h>
  82. #include <scsi/scsi_transport_srp.h>
  83. #include "ibmvscsi.h"
  84. /* The values below are somewhat arbitrary default values, but
  85. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  86. * Note that there are 3 bits of channel value, 6 bits of id, and
  87. * 5 bits of LUN.
  88. */
  89. static int max_id = 64;
  90. static int max_channel = 3;
  91. static int init_timeout = 300;
  92. static int login_timeout = 60;
  93. static int info_timeout = 30;
  94. static int abort_timeout = 60;
  95. static int reset_timeout = 60;
  96. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  97. static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
  98. static int fast_fail = 1;
  99. static int client_reserve = 1;
  100. static struct scsi_transport_template *ibmvscsi_transport_template;
  101. #define IBMVSCSI_VERSION "1.5.9"
  102. static struct ibmvscsi_ops *ibmvscsi_ops;
  103. MODULE_DESCRIPTION("IBM Virtual SCSI");
  104. MODULE_AUTHOR("Dave Boutcher");
  105. MODULE_LICENSE("GPL");
  106. MODULE_VERSION(IBMVSCSI_VERSION);
  107. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  108. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  109. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  110. MODULE_PARM_DESC(max_channel, "Largest channel value");
  111. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  112. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  113. module_param_named(max_requests, max_requests, int, S_IRUGO);
  114. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  115. module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
  116. MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
  117. module_param_named(client_reserve, client_reserve, int, S_IRUGO );
  118. MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
  119. /* ------------------------------------------------------------
  120. * Routines for the event pool and event structs
  121. */
  122. /**
  123. * initialize_event_pool: - Allocates and initializes the event pool for a host
  124. * @pool: event_pool to be initialized
  125. * @size: Number of events in pool
  126. * @hostdata: ibmvscsi_host_data who owns the event pool
  127. *
  128. * Returns zero on success.
  129. */
  130. static int initialize_event_pool(struct event_pool *pool,
  131. int size, struct ibmvscsi_host_data *hostdata)
  132. {
  133. int i;
  134. pool->size = size;
  135. pool->next = 0;
  136. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  137. if (!pool->events)
  138. return -ENOMEM;
  139. pool->iu_storage =
  140. dma_alloc_coherent(hostdata->dev,
  141. pool->size * sizeof(*pool->iu_storage),
  142. &pool->iu_token, 0);
  143. if (!pool->iu_storage) {
  144. kfree(pool->events);
  145. return -ENOMEM;
  146. }
  147. for (i = 0; i < pool->size; ++i) {
  148. struct srp_event_struct *evt = &pool->events[i];
  149. memset(&evt->crq, 0x00, sizeof(evt->crq));
  150. atomic_set(&evt->free, 1);
  151. evt->crq.valid = 0x80;
  152. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  153. evt->crq.IU_data_ptr = pool->iu_token +
  154. sizeof(*evt->xfer_iu) * i;
  155. evt->xfer_iu = pool->iu_storage + i;
  156. evt->hostdata = hostdata;
  157. evt->ext_list = NULL;
  158. evt->ext_list_token = 0;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * release_event_pool: - Frees memory of an event pool of a host
  164. * @pool: event_pool to be released
  165. * @hostdata: ibmvscsi_host_data who owns the even pool
  166. *
  167. * Returns zero on success.
  168. */
  169. static void release_event_pool(struct event_pool *pool,
  170. struct ibmvscsi_host_data *hostdata)
  171. {
  172. int i, in_use = 0;
  173. for (i = 0; i < pool->size; ++i) {
  174. if (atomic_read(&pool->events[i].free) != 1)
  175. ++in_use;
  176. if (pool->events[i].ext_list) {
  177. dma_free_coherent(hostdata->dev,
  178. SG_ALL * sizeof(struct srp_direct_buf),
  179. pool->events[i].ext_list,
  180. pool->events[i].ext_list_token);
  181. }
  182. }
  183. if (in_use)
  184. dev_warn(hostdata->dev, "releasing event pool with %d "
  185. "events still in use?\n", in_use);
  186. kfree(pool->events);
  187. dma_free_coherent(hostdata->dev,
  188. pool->size * sizeof(*pool->iu_storage),
  189. pool->iu_storage, pool->iu_token);
  190. }
  191. /**
  192. * valid_event_struct: - Determines if event is valid.
  193. * @pool: event_pool that contains the event
  194. * @evt: srp_event_struct to be checked for validity
  195. *
  196. * Returns zero if event is invalid, one otherwise.
  197. */
  198. static int valid_event_struct(struct event_pool *pool,
  199. struct srp_event_struct *evt)
  200. {
  201. int index = evt - pool->events;
  202. if (index < 0 || index >= pool->size) /* outside of bounds */
  203. return 0;
  204. if (evt != pool->events + index) /* unaligned */
  205. return 0;
  206. return 1;
  207. }
  208. /**
  209. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  210. * @pool: event_pool that contains the event
  211. * @evt: srp_event_struct to be modified
  212. *
  213. */
  214. static void free_event_struct(struct event_pool *pool,
  215. struct srp_event_struct *evt)
  216. {
  217. if (!valid_event_struct(pool, evt)) {
  218. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  219. "(not in pool %p)\n", evt, pool->events);
  220. return;
  221. }
  222. if (atomic_inc_return(&evt->free) != 1) {
  223. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  224. "which is not in use!\n", evt);
  225. return;
  226. }
  227. }
  228. /**
  229. * get_evt_struct: - Gets the next free event in pool
  230. * @pool: event_pool that contains the events to be searched
  231. *
  232. * Returns the next event in "free" state, and NULL if none are free.
  233. * Note that no synchronization is done here, we assume the host_lock
  234. * will syncrhonze things.
  235. */
  236. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  237. {
  238. int i;
  239. int poolsize = pool->size;
  240. int offset = pool->next;
  241. for (i = 0; i < poolsize; i++) {
  242. offset = (offset + 1) % poolsize;
  243. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  244. pool->next = offset;
  245. return &pool->events[offset];
  246. }
  247. }
  248. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  249. return NULL;
  250. }
  251. /**
  252. * init_event_struct: Initialize fields in an event struct that are always
  253. * required.
  254. * @evt: The event
  255. * @done: Routine to call when the event is responded to
  256. * @format: SRP or MAD format
  257. * @timeout: timeout value set in the CRQ
  258. */
  259. static void init_event_struct(struct srp_event_struct *evt_struct,
  260. void (*done) (struct srp_event_struct *),
  261. u8 format,
  262. int timeout)
  263. {
  264. evt_struct->cmnd = NULL;
  265. evt_struct->cmnd_done = NULL;
  266. evt_struct->sync_srp = NULL;
  267. evt_struct->crq.format = format;
  268. evt_struct->crq.timeout = timeout;
  269. evt_struct->done = done;
  270. }
  271. /* ------------------------------------------------------------
  272. * Routines for receiving SCSI responses from the hosting partition
  273. */
  274. /**
  275. * set_srp_direction: Set the fields in the srp related to data
  276. * direction and number of buffers based on the direction in
  277. * the scsi_cmnd and the number of buffers
  278. */
  279. static void set_srp_direction(struct scsi_cmnd *cmd,
  280. struct srp_cmd *srp_cmd,
  281. int numbuf)
  282. {
  283. u8 fmt;
  284. if (numbuf == 0)
  285. return;
  286. if (numbuf == 1)
  287. fmt = SRP_DATA_DESC_DIRECT;
  288. else {
  289. fmt = SRP_DATA_DESC_INDIRECT;
  290. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  291. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  292. srp_cmd->data_out_desc_cnt = numbuf;
  293. else
  294. srp_cmd->data_in_desc_cnt = numbuf;
  295. }
  296. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  297. srp_cmd->buf_fmt = fmt << 4;
  298. else
  299. srp_cmd->buf_fmt = fmt;
  300. }
  301. /**
  302. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  303. * @cmd: srp_cmd whose additional_data member will be unmapped
  304. * @dev: device for which the memory is mapped
  305. *
  306. */
  307. static void unmap_cmd_data(struct srp_cmd *cmd,
  308. struct srp_event_struct *evt_struct,
  309. struct device *dev)
  310. {
  311. u8 out_fmt, in_fmt;
  312. out_fmt = cmd->buf_fmt >> 4;
  313. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  314. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  315. return;
  316. if (evt_struct->cmnd)
  317. scsi_dma_unmap(evt_struct->cmnd);
  318. }
  319. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  320. struct srp_direct_buf *md)
  321. {
  322. int i;
  323. struct scatterlist *sg;
  324. u64 total_length = 0;
  325. scsi_for_each_sg(cmd, sg, nseg, i) {
  326. struct srp_direct_buf *descr = md + i;
  327. descr->va = sg_dma_address(sg);
  328. descr->len = sg_dma_len(sg);
  329. descr->key = 0;
  330. total_length += sg_dma_len(sg);
  331. }
  332. return total_length;
  333. }
  334. /**
  335. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  336. * @cmd: Scsi_Cmnd with the scatterlist
  337. * @srp_cmd: srp_cmd that contains the memory descriptor
  338. * @dev: device for which to map dma memory
  339. *
  340. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  341. * Returns 1 on success.
  342. */
  343. static int map_sg_data(struct scsi_cmnd *cmd,
  344. struct srp_event_struct *evt_struct,
  345. struct srp_cmd *srp_cmd, struct device *dev)
  346. {
  347. int sg_mapped;
  348. u64 total_length = 0;
  349. struct srp_direct_buf *data =
  350. (struct srp_direct_buf *) srp_cmd->add_data;
  351. struct srp_indirect_buf *indirect =
  352. (struct srp_indirect_buf *) data;
  353. sg_mapped = scsi_dma_map(cmd);
  354. if (!sg_mapped)
  355. return 1;
  356. else if (sg_mapped < 0)
  357. return 0;
  358. set_srp_direction(cmd, srp_cmd, sg_mapped);
  359. /* special case; we can use a single direct descriptor */
  360. if (sg_mapped == 1) {
  361. map_sg_list(cmd, sg_mapped, data);
  362. return 1;
  363. }
  364. indirect->table_desc.va = 0;
  365. indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
  366. indirect->table_desc.key = 0;
  367. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  368. total_length = map_sg_list(cmd, sg_mapped,
  369. &indirect->desc_list[0]);
  370. indirect->len = total_length;
  371. return 1;
  372. }
  373. /* get indirect table */
  374. if (!evt_struct->ext_list) {
  375. evt_struct->ext_list = (struct srp_direct_buf *)
  376. dma_alloc_coherent(dev,
  377. SG_ALL * sizeof(struct srp_direct_buf),
  378. &evt_struct->ext_list_token, 0);
  379. if (!evt_struct->ext_list) {
  380. if (!firmware_has_feature(FW_FEATURE_CMO))
  381. sdev_printk(KERN_ERR, cmd->device,
  382. "Can't allocate memory "
  383. "for indirect table\n");
  384. scsi_dma_unmap(cmd);
  385. return 0;
  386. }
  387. }
  388. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  389. indirect->len = total_length;
  390. indirect->table_desc.va = evt_struct->ext_list_token;
  391. indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
  392. memcpy(indirect->desc_list, evt_struct->ext_list,
  393. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  394. return 1;
  395. }
  396. /**
  397. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  398. * @cmd: struct scsi_cmnd with the memory to be mapped
  399. * @srp_cmd: srp_cmd that contains the memory descriptor
  400. * @dev: dma device for which to map dma memory
  401. *
  402. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  403. * Returns 1 on success.
  404. */
  405. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  406. struct srp_event_struct *evt_struct,
  407. struct srp_cmd *srp_cmd, struct device *dev)
  408. {
  409. switch (cmd->sc_data_direction) {
  410. case DMA_FROM_DEVICE:
  411. case DMA_TO_DEVICE:
  412. break;
  413. case DMA_NONE:
  414. return 1;
  415. case DMA_BIDIRECTIONAL:
  416. sdev_printk(KERN_ERR, cmd->device,
  417. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  418. return 0;
  419. default:
  420. sdev_printk(KERN_ERR, cmd->device,
  421. "Unknown data direction 0x%02x; can't map!\n",
  422. cmd->sc_data_direction);
  423. return 0;
  424. }
  425. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  426. }
  427. /**
  428. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  429. * @hostdata: the adapter
  430. */
  431. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  432. {
  433. struct srp_event_struct *evt;
  434. unsigned long flags;
  435. spin_lock_irqsave(hostdata->host->host_lock, flags);
  436. while (!list_empty(&hostdata->sent)) {
  437. evt = list_first_entry(&hostdata->sent, struct srp_event_struct, list);
  438. list_del(&evt->list);
  439. del_timer(&evt->timer);
  440. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  441. if (evt->cmnd) {
  442. evt->cmnd->result = (error_code << 16);
  443. unmap_cmd_data(&evt->iu.srp.cmd, evt,
  444. evt->hostdata->dev);
  445. if (evt->cmnd_done)
  446. evt->cmnd_done(evt->cmnd);
  447. } else if (evt->done)
  448. evt->done(evt);
  449. free_event_struct(&evt->hostdata->pool, evt);
  450. spin_lock_irqsave(hostdata->host->host_lock, flags);
  451. }
  452. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  453. }
  454. /**
  455. * ibmvscsi_reset_host - Reset the connection to the server
  456. * @hostdata: struct ibmvscsi_host_data to reset
  457. */
  458. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  459. {
  460. scsi_block_requests(hostdata->host);
  461. atomic_set(&hostdata->request_limit, 0);
  462. purge_requests(hostdata, DID_ERROR);
  463. hostdata->reset_crq = 1;
  464. wake_up(&hostdata->work_wait_q);
  465. }
  466. /**
  467. * ibmvscsi_timeout - Internal command timeout handler
  468. * @evt_struct: struct srp_event_struct that timed out
  469. *
  470. * Called when an internally generated command times out
  471. */
  472. static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
  473. {
  474. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  475. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  476. evt_struct->iu.srp.cmd.opcode);
  477. ibmvscsi_reset_host(hostdata);
  478. }
  479. /* ------------------------------------------------------------
  480. * Routines for sending and receiving SRPs
  481. */
  482. /**
  483. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  484. * @evt_struct: evt_struct to be sent
  485. * @hostdata: ibmvscsi_host_data of host
  486. * @timeout: timeout in seconds - 0 means do not time command
  487. *
  488. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  489. * Note that this routine assumes that host_lock is held for synchronization
  490. */
  491. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  492. struct ibmvscsi_host_data *hostdata,
  493. unsigned long timeout)
  494. {
  495. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  496. int request_status = 0;
  497. int rc;
  498. int srp_req = 0;
  499. /* If we have exhausted our request limit, just fail this request,
  500. * unless it is for a reset or abort.
  501. * Note that there are rare cases involving driver generated requests
  502. * (such as task management requests) that the mid layer may think we
  503. * can handle more requests (can_queue) when we actually can't
  504. */
  505. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  506. srp_req = 1;
  507. request_status =
  508. atomic_dec_if_positive(&hostdata->request_limit);
  509. /* If request limit was -1 when we started, it is now even
  510. * less than that
  511. */
  512. if (request_status < -1)
  513. goto send_error;
  514. /* Otherwise, we may have run out of requests. */
  515. /* If request limit was 0 when we started the adapter is in the
  516. * process of performing a login with the server adapter, or
  517. * we may have run out of requests.
  518. */
  519. else if (request_status == -1 &&
  520. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  521. goto send_busy;
  522. /* Abort and reset calls should make it through.
  523. * Nothing except abort and reset should use the last two
  524. * slots unless we had two or less to begin with.
  525. */
  526. else if (request_status < 2 &&
  527. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  528. /* In the case that we have less than two requests
  529. * available, check the server limit as a combination
  530. * of the request limit and the number of requests
  531. * in-flight (the size of the send list). If the
  532. * server limit is greater than 2, return busy so
  533. * that the last two are reserved for reset and abort.
  534. */
  535. int server_limit = request_status;
  536. struct srp_event_struct *tmp_evt;
  537. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  538. server_limit++;
  539. }
  540. if (server_limit > 2)
  541. goto send_busy;
  542. }
  543. }
  544. /* Copy the IU into the transfer area */
  545. *evt_struct->xfer_iu = evt_struct->iu;
  546. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  547. /* Add this to the sent list. We need to do this
  548. * before we actually send
  549. * in case it comes back REALLY fast
  550. */
  551. list_add_tail(&evt_struct->list, &hostdata->sent);
  552. init_timer(&evt_struct->timer);
  553. if (timeout) {
  554. evt_struct->timer.data = (unsigned long) evt_struct;
  555. evt_struct->timer.expires = jiffies + (timeout * HZ);
  556. evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
  557. add_timer(&evt_struct->timer);
  558. }
  559. if ((rc =
  560. ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  561. list_del(&evt_struct->list);
  562. del_timer(&evt_struct->timer);
  563. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  564. * Firmware will send a CRQ with a transport event (0xFF) to
  565. * tell this client what has happened to the transport. This
  566. * will be handled in ibmvscsi_handle_crq()
  567. */
  568. if (rc == H_CLOSED) {
  569. dev_warn(hostdata->dev, "send warning. "
  570. "Receive queue closed, will retry.\n");
  571. goto send_busy;
  572. }
  573. dev_err(hostdata->dev, "send error %d\n", rc);
  574. if (srp_req)
  575. atomic_inc(&hostdata->request_limit);
  576. goto send_error;
  577. }
  578. return 0;
  579. send_busy:
  580. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  581. free_event_struct(&hostdata->pool, evt_struct);
  582. if (srp_req && request_status != -1)
  583. atomic_inc(&hostdata->request_limit);
  584. return SCSI_MLQUEUE_HOST_BUSY;
  585. send_error:
  586. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  587. if (evt_struct->cmnd != NULL) {
  588. evt_struct->cmnd->result = DID_ERROR << 16;
  589. evt_struct->cmnd_done(evt_struct->cmnd);
  590. } else if (evt_struct->done)
  591. evt_struct->done(evt_struct);
  592. free_event_struct(&hostdata->pool, evt_struct);
  593. return 0;
  594. }
  595. /**
  596. * handle_cmd_rsp: - Handle responses from commands
  597. * @evt_struct: srp_event_struct to be handled
  598. *
  599. * Used as a callback by when sending scsi cmds.
  600. * Gets called by ibmvscsi_handle_crq()
  601. */
  602. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  603. {
  604. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  605. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  606. if (unlikely(rsp->opcode != SRP_RSP)) {
  607. if (printk_ratelimit())
  608. dev_warn(evt_struct->hostdata->dev,
  609. "bad SRP RSP type %d\n", rsp->opcode);
  610. }
  611. if (cmnd) {
  612. cmnd->result |= rsp->status;
  613. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  614. memcpy(cmnd->sense_buffer,
  615. rsp->data,
  616. rsp->sense_data_len);
  617. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  618. evt_struct,
  619. evt_struct->hostdata->dev);
  620. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  621. scsi_set_resid(cmnd, rsp->data_out_res_cnt);
  622. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  623. scsi_set_resid(cmnd, rsp->data_in_res_cnt);
  624. }
  625. if (evt_struct->cmnd_done)
  626. evt_struct->cmnd_done(cmnd);
  627. }
  628. /**
  629. * lun_from_dev: - Returns the lun of the scsi device
  630. * @dev: struct scsi_device
  631. *
  632. */
  633. static inline u16 lun_from_dev(struct scsi_device *dev)
  634. {
  635. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  636. }
  637. /**
  638. * ibmvscsi_queue: - The queuecommand function of the scsi template
  639. * @cmd: struct scsi_cmnd to be executed
  640. * @done: Callback function to be called when cmd is completed
  641. */
  642. static int ibmvscsi_queuecommand_lck(struct scsi_cmnd *cmnd,
  643. void (*done) (struct scsi_cmnd *))
  644. {
  645. struct srp_cmd *srp_cmd;
  646. struct srp_event_struct *evt_struct;
  647. struct srp_indirect_buf *indirect;
  648. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  649. u16 lun = lun_from_dev(cmnd->device);
  650. u8 out_fmt, in_fmt;
  651. cmnd->result = (DID_OK << 16);
  652. evt_struct = get_event_struct(&hostdata->pool);
  653. if (!evt_struct)
  654. return SCSI_MLQUEUE_HOST_BUSY;
  655. /* Set up the actual SRP IU */
  656. srp_cmd = &evt_struct->iu.srp.cmd;
  657. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  658. srp_cmd->opcode = SRP_CMD;
  659. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
  660. srp_cmd->lun = ((u64) lun) << 48;
  661. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  662. if (!firmware_has_feature(FW_FEATURE_CMO))
  663. sdev_printk(KERN_ERR, cmnd->device,
  664. "couldn't convert cmd to srp_cmd\n");
  665. free_event_struct(&hostdata->pool, evt_struct);
  666. return SCSI_MLQUEUE_HOST_BUSY;
  667. }
  668. init_event_struct(evt_struct,
  669. handle_cmd_rsp,
  670. VIOSRP_SRP_FORMAT,
  671. cmnd->request->timeout/HZ);
  672. evt_struct->cmnd = cmnd;
  673. evt_struct->cmnd_done = done;
  674. /* Fix up dma address of the buffer itself */
  675. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  676. out_fmt = srp_cmd->buf_fmt >> 4;
  677. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  678. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  679. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  680. indirect->table_desc.va == 0) {
  681. indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
  682. offsetof(struct srp_cmd, add_data) +
  683. offsetof(struct srp_indirect_buf, desc_list);
  684. }
  685. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  686. }
  687. static DEF_SCSI_QCMD(ibmvscsi_queuecommand)
  688. /* ------------------------------------------------------------
  689. * Routines for driver initialization
  690. */
  691. /**
  692. * map_persist_bufs: - Pre-map persistent data for adapter logins
  693. * @hostdata: ibmvscsi_host_data of host
  694. *
  695. * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
  696. * Return 1 on error, 0 on success.
  697. */
  698. static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
  699. {
  700. hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
  701. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  702. if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
  703. dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
  704. return 1;
  705. }
  706. hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
  707. &hostdata->madapter_info,
  708. sizeof(hostdata->madapter_info),
  709. DMA_BIDIRECTIONAL);
  710. if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
  711. dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
  712. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  713. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  714. return 1;
  715. }
  716. return 0;
  717. }
  718. /**
  719. * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
  720. * @hostdata: ibmvscsi_host_data of host
  721. *
  722. * Unmap the capabilities and adapter info DMA buffers
  723. */
  724. static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
  725. {
  726. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  727. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  728. dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
  729. sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
  730. }
  731. /**
  732. * login_rsp: - Handle response to SRP login request
  733. * @evt_struct: srp_event_struct with the response
  734. *
  735. * Used as a "done" callback by when sending srp_login. Gets called
  736. * by ibmvscsi_handle_crq()
  737. */
  738. static void login_rsp(struct srp_event_struct *evt_struct)
  739. {
  740. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  741. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  742. case SRP_LOGIN_RSP: /* it worked! */
  743. break;
  744. case SRP_LOGIN_REJ: /* refused! */
  745. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  746. evt_struct->xfer_iu->srp.login_rej.reason);
  747. /* Login failed. */
  748. atomic_set(&hostdata->request_limit, -1);
  749. return;
  750. default:
  751. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  752. evt_struct->xfer_iu->srp.login_rsp.opcode);
  753. /* Login failed. */
  754. atomic_set(&hostdata->request_limit, -1);
  755. return;
  756. }
  757. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  758. hostdata->client_migrated = 0;
  759. /* Now we know what the real request-limit is.
  760. * This value is set rather than added to request_limit because
  761. * request_limit could have been set to -1 by this client.
  762. */
  763. atomic_set(&hostdata->request_limit,
  764. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
  765. /* If we had any pending I/Os, kick them */
  766. scsi_unblock_requests(hostdata->host);
  767. }
  768. /**
  769. * send_srp_login: - Sends the srp login
  770. * @hostdata: ibmvscsi_host_data of host
  771. *
  772. * Returns zero if successful.
  773. */
  774. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  775. {
  776. int rc;
  777. unsigned long flags;
  778. struct srp_login_req *login;
  779. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  780. BUG_ON(!evt_struct);
  781. init_event_struct(evt_struct, login_rsp,
  782. VIOSRP_SRP_FORMAT, login_timeout);
  783. login = &evt_struct->iu.srp.login_req;
  784. memset(login, 0, sizeof(*login));
  785. login->opcode = SRP_LOGIN_REQ;
  786. login->req_it_iu_len = sizeof(union srp_iu);
  787. login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
  788. spin_lock_irqsave(hostdata->host->host_lock, flags);
  789. /* Start out with a request limit of 0, since this is negotiated in
  790. * the login request we are just sending and login requests always
  791. * get sent by the driver regardless of request_limit.
  792. */
  793. atomic_set(&hostdata->request_limit, 0);
  794. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
  795. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  796. dev_info(hostdata->dev, "sent SRP login\n");
  797. return rc;
  798. };
  799. /**
  800. * capabilities_rsp: - Handle response to MAD adapter capabilities request
  801. * @evt_struct: srp_event_struct with the response
  802. *
  803. * Used as a "done" callback by when sending adapter_info.
  804. */
  805. static void capabilities_rsp(struct srp_event_struct *evt_struct)
  806. {
  807. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  808. if (evt_struct->xfer_iu->mad.capabilities.common.status) {
  809. dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
  810. evt_struct->xfer_iu->mad.capabilities.common.status);
  811. } else {
  812. if (hostdata->caps.migration.common.server_support != SERVER_SUPPORTS_CAP)
  813. dev_info(hostdata->dev, "Partition migration not supported\n");
  814. if (client_reserve) {
  815. if (hostdata->caps.reserve.common.server_support ==
  816. SERVER_SUPPORTS_CAP)
  817. dev_info(hostdata->dev, "Client reserve enabled\n");
  818. else
  819. dev_info(hostdata->dev, "Client reserve not supported\n");
  820. }
  821. }
  822. send_srp_login(hostdata);
  823. }
  824. /**
  825. * send_mad_capabilities: - Sends the mad capabilities request
  826. * and stores the result so it can be retrieved with
  827. * @hostdata: ibmvscsi_host_data of host
  828. */
  829. static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
  830. {
  831. struct viosrp_capabilities *req;
  832. struct srp_event_struct *evt_struct;
  833. unsigned long flags;
  834. struct device_node *of_node = hostdata->dev->of_node;
  835. const char *location;
  836. evt_struct = get_event_struct(&hostdata->pool);
  837. BUG_ON(!evt_struct);
  838. init_event_struct(evt_struct, capabilities_rsp,
  839. VIOSRP_MAD_FORMAT, info_timeout);
  840. req = &evt_struct->iu.mad.capabilities;
  841. memset(req, 0, sizeof(*req));
  842. hostdata->caps.flags = CAP_LIST_SUPPORTED;
  843. if (hostdata->client_migrated)
  844. hostdata->caps.flags |= CLIENT_MIGRATED;
  845. strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
  846. sizeof(hostdata->caps.name));
  847. hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
  848. location = of_get_property(of_node, "ibm,loc-code", NULL);
  849. location = location ? location : dev_name(hostdata->dev);
  850. strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
  851. hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
  852. req->common.type = VIOSRP_CAPABILITIES_TYPE;
  853. req->buffer = hostdata->caps_addr;
  854. hostdata->caps.migration.common.cap_type = MIGRATION_CAPABILITIES;
  855. hostdata->caps.migration.common.length = sizeof(hostdata->caps.migration);
  856. hostdata->caps.migration.common.server_support = SERVER_SUPPORTS_CAP;
  857. hostdata->caps.migration.ecl = 1;
  858. if (client_reserve) {
  859. hostdata->caps.reserve.common.cap_type = RESERVATION_CAPABILITIES;
  860. hostdata->caps.reserve.common.length = sizeof(hostdata->caps.reserve);
  861. hostdata->caps.reserve.common.server_support = SERVER_SUPPORTS_CAP;
  862. hostdata->caps.reserve.type = CLIENT_RESERVE_SCSI_2;
  863. req->common.length = sizeof(hostdata->caps);
  864. } else
  865. req->common.length = sizeof(hostdata->caps) - sizeof(hostdata->caps.reserve);
  866. spin_lock_irqsave(hostdata->host->host_lock, flags);
  867. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  868. dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
  869. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  870. };
  871. /**
  872. * fast_fail_rsp: - Handle response to MAD enable fast fail
  873. * @evt_struct: srp_event_struct with the response
  874. *
  875. * Used as a "done" callback by when sending enable fast fail. Gets called
  876. * by ibmvscsi_handle_crq()
  877. */
  878. static void fast_fail_rsp(struct srp_event_struct *evt_struct)
  879. {
  880. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  881. u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;
  882. if (status == VIOSRP_MAD_NOT_SUPPORTED)
  883. dev_err(hostdata->dev, "fast_fail not supported in server\n");
  884. else if (status == VIOSRP_MAD_FAILED)
  885. dev_err(hostdata->dev, "fast_fail request failed\n");
  886. else if (status != VIOSRP_MAD_SUCCESS)
  887. dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
  888. send_mad_capabilities(hostdata);
  889. }
  890. /**
  891. * init_host - Start host initialization
  892. * @hostdata: ibmvscsi_host_data of host
  893. *
  894. * Returns zero if successful.
  895. */
  896. static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
  897. {
  898. int rc;
  899. unsigned long flags;
  900. struct viosrp_fast_fail *fast_fail_mad;
  901. struct srp_event_struct *evt_struct;
  902. if (!fast_fail) {
  903. send_mad_capabilities(hostdata);
  904. return 0;
  905. }
  906. evt_struct = get_event_struct(&hostdata->pool);
  907. BUG_ON(!evt_struct);
  908. init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
  909. fast_fail_mad = &evt_struct->iu.mad.fast_fail;
  910. memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
  911. fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
  912. fast_fail_mad->common.length = sizeof(*fast_fail_mad);
  913. spin_lock_irqsave(hostdata->host->host_lock, flags);
  914. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  915. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  916. return rc;
  917. }
  918. /**
  919. * adapter_info_rsp: - Handle response to MAD adapter info request
  920. * @evt_struct: srp_event_struct with the response
  921. *
  922. * Used as a "done" callback by when sending adapter_info. Gets called
  923. * by ibmvscsi_handle_crq()
  924. */
  925. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  926. {
  927. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  928. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  929. dev_err(hostdata->dev, "error %d getting adapter info\n",
  930. evt_struct->xfer_iu->mad.adapter_info.common.status);
  931. } else {
  932. dev_info(hostdata->dev, "host srp version: %s, "
  933. "host partition %s (%d), OS %d, max io %u\n",
  934. hostdata->madapter_info.srp_version,
  935. hostdata->madapter_info.partition_name,
  936. hostdata->madapter_info.partition_number,
  937. hostdata->madapter_info.os_type,
  938. hostdata->madapter_info.port_max_txu[0]);
  939. if (hostdata->madapter_info.port_max_txu[0])
  940. hostdata->host->max_sectors =
  941. hostdata->madapter_info.port_max_txu[0] >> 9;
  942. if (hostdata->madapter_info.os_type == 3 &&
  943. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  944. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  945. hostdata->madapter_info.srp_version);
  946. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  947. MAX_INDIRECT_BUFS);
  948. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  949. }
  950. if (hostdata->madapter_info.os_type == 3) {
  951. enable_fast_fail(hostdata);
  952. return;
  953. }
  954. }
  955. send_srp_login(hostdata);
  956. }
  957. /**
  958. * send_mad_adapter_info: - Sends the mad adapter info request
  959. * and stores the result so it can be retrieved with
  960. * sysfs. We COULD consider causing a failure if the
  961. * returned SRP version doesn't match ours.
  962. * @hostdata: ibmvscsi_host_data of host
  963. *
  964. * Returns zero if successful.
  965. */
  966. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  967. {
  968. struct viosrp_adapter_info *req;
  969. struct srp_event_struct *evt_struct;
  970. unsigned long flags;
  971. evt_struct = get_event_struct(&hostdata->pool);
  972. BUG_ON(!evt_struct);
  973. init_event_struct(evt_struct,
  974. adapter_info_rsp,
  975. VIOSRP_MAD_FORMAT,
  976. info_timeout);
  977. req = &evt_struct->iu.mad.adapter_info;
  978. memset(req, 0x00, sizeof(*req));
  979. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  980. req->common.length = sizeof(hostdata->madapter_info);
  981. req->buffer = hostdata->adapter_info_addr;
  982. spin_lock_irqsave(hostdata->host->host_lock, flags);
  983. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  984. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  985. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  986. };
  987. /**
  988. * init_adapter: Start virtual adapter initialization sequence
  989. *
  990. */
  991. static void init_adapter(struct ibmvscsi_host_data *hostdata)
  992. {
  993. send_mad_adapter_info(hostdata);
  994. }
  995. /**
  996. * sync_completion: Signal that a synchronous command has completed
  997. * Note that after returning from this call, the evt_struct is freed.
  998. * the caller waiting on this completion shouldn't touch the evt_struct
  999. * again.
  1000. */
  1001. static void sync_completion(struct srp_event_struct *evt_struct)
  1002. {
  1003. /* copy the response back */
  1004. if (evt_struct->sync_srp)
  1005. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  1006. complete(&evt_struct->comp);
  1007. }
  1008. /**
  1009. * ibmvscsi_abort: Abort a command...from scsi host template
  1010. * send this over to the server and wait synchronously for the response
  1011. */
  1012. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  1013. {
  1014. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1015. struct srp_tsk_mgmt *tsk_mgmt;
  1016. struct srp_event_struct *evt;
  1017. struct srp_event_struct *tmp_evt, *found_evt;
  1018. union viosrp_iu srp_rsp;
  1019. int rsp_rc;
  1020. unsigned long flags;
  1021. u16 lun = lun_from_dev(cmd->device);
  1022. unsigned long wait_switch = 0;
  1023. /* First, find this command in our sent list so we can figure
  1024. * out the correct tag
  1025. */
  1026. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1027. wait_switch = jiffies + (init_timeout * HZ);
  1028. do {
  1029. found_evt = NULL;
  1030. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1031. if (tmp_evt->cmnd == cmd) {
  1032. found_evt = tmp_evt;
  1033. break;
  1034. }
  1035. }
  1036. if (!found_evt) {
  1037. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1038. return SUCCESS;
  1039. }
  1040. evt = get_event_struct(&hostdata->pool);
  1041. if (evt == NULL) {
  1042. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1043. sdev_printk(KERN_ERR, cmd->device,
  1044. "failed to allocate abort event\n");
  1045. return FAILED;
  1046. }
  1047. init_event_struct(evt,
  1048. sync_completion,
  1049. VIOSRP_SRP_FORMAT,
  1050. abort_timeout);
  1051. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1052. /* Set up an abort SRP command */
  1053. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1054. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1055. tsk_mgmt->lun = ((u64) lun) << 48;
  1056. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  1057. tsk_mgmt->task_tag = (u64) found_evt;
  1058. evt->sync_srp = &srp_rsp;
  1059. init_completion(&evt->comp);
  1060. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
  1061. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1062. break;
  1063. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1064. msleep(10);
  1065. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1066. } while (time_before(jiffies, wait_switch));
  1067. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1068. if (rsp_rc != 0) {
  1069. sdev_printk(KERN_ERR, cmd->device,
  1070. "failed to send abort() event. rc=%d\n", rsp_rc);
  1071. return FAILED;
  1072. }
  1073. sdev_printk(KERN_INFO, cmd->device,
  1074. "aborting command. lun 0x%llx, tag 0x%llx\n",
  1075. (((u64) lun) << 48), (u64) found_evt);
  1076. wait_for_completion(&evt->comp);
  1077. /* make sure we got a good response */
  1078. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1079. if (printk_ratelimit())
  1080. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  1081. srp_rsp.srp.rsp.opcode);
  1082. return FAILED;
  1083. }
  1084. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1085. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1086. else
  1087. rsp_rc = srp_rsp.srp.rsp.status;
  1088. if (rsp_rc) {
  1089. if (printk_ratelimit())
  1090. sdev_printk(KERN_WARNING, cmd->device,
  1091. "abort code %d for task tag 0x%llx\n",
  1092. rsp_rc, tsk_mgmt->task_tag);
  1093. return FAILED;
  1094. }
  1095. /* Because we dropped the spinlock above, it's possible
  1096. * The event is no longer in our list. Make sure it didn't
  1097. * complete while we were aborting
  1098. */
  1099. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1100. found_evt = NULL;
  1101. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1102. if (tmp_evt->cmnd == cmd) {
  1103. found_evt = tmp_evt;
  1104. break;
  1105. }
  1106. }
  1107. if (found_evt == NULL) {
  1108. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1109. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
  1110. tsk_mgmt->task_tag);
  1111. return SUCCESS;
  1112. }
  1113. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
  1114. tsk_mgmt->task_tag);
  1115. cmd->result = (DID_ABORT << 16);
  1116. list_del(&found_evt->list);
  1117. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  1118. found_evt->hostdata->dev);
  1119. free_event_struct(&found_evt->hostdata->pool, found_evt);
  1120. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1121. atomic_inc(&hostdata->request_limit);
  1122. return SUCCESS;
  1123. }
  1124. /**
  1125. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  1126. * template send this over to the server and wait synchronously for the
  1127. * response
  1128. */
  1129. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  1130. {
  1131. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1132. struct srp_tsk_mgmt *tsk_mgmt;
  1133. struct srp_event_struct *evt;
  1134. struct srp_event_struct *tmp_evt, *pos;
  1135. union viosrp_iu srp_rsp;
  1136. int rsp_rc;
  1137. unsigned long flags;
  1138. u16 lun = lun_from_dev(cmd->device);
  1139. unsigned long wait_switch = 0;
  1140. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1141. wait_switch = jiffies + (init_timeout * HZ);
  1142. do {
  1143. evt = get_event_struct(&hostdata->pool);
  1144. if (evt == NULL) {
  1145. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1146. sdev_printk(KERN_ERR, cmd->device,
  1147. "failed to allocate reset event\n");
  1148. return FAILED;
  1149. }
  1150. init_event_struct(evt,
  1151. sync_completion,
  1152. VIOSRP_SRP_FORMAT,
  1153. reset_timeout);
  1154. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1155. /* Set up a lun reset SRP command */
  1156. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1157. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1158. tsk_mgmt->lun = ((u64) lun) << 48;
  1159. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  1160. evt->sync_srp = &srp_rsp;
  1161. init_completion(&evt->comp);
  1162. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
  1163. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1164. break;
  1165. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1166. msleep(10);
  1167. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1168. } while (time_before(jiffies, wait_switch));
  1169. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1170. if (rsp_rc != 0) {
  1171. sdev_printk(KERN_ERR, cmd->device,
  1172. "failed to send reset event. rc=%d\n", rsp_rc);
  1173. return FAILED;
  1174. }
  1175. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
  1176. (((u64) lun) << 48));
  1177. wait_for_completion(&evt->comp);
  1178. /* make sure we got a good response */
  1179. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1180. if (printk_ratelimit())
  1181. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1182. srp_rsp.srp.rsp.opcode);
  1183. return FAILED;
  1184. }
  1185. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1186. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1187. else
  1188. rsp_rc = srp_rsp.srp.rsp.status;
  1189. if (rsp_rc) {
  1190. if (printk_ratelimit())
  1191. sdev_printk(KERN_WARNING, cmd->device,
  1192. "reset code %d for task tag 0x%llx\n",
  1193. rsp_rc, tsk_mgmt->task_tag);
  1194. return FAILED;
  1195. }
  1196. /* We need to find all commands for this LUN that have not yet been
  1197. * responded to, and fail them with DID_RESET
  1198. */
  1199. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1200. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1201. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1202. if (tmp_evt->cmnd)
  1203. tmp_evt->cmnd->result = (DID_RESET << 16);
  1204. list_del(&tmp_evt->list);
  1205. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1206. tmp_evt->hostdata->dev);
  1207. free_event_struct(&tmp_evt->hostdata->pool,
  1208. tmp_evt);
  1209. atomic_inc(&hostdata->request_limit);
  1210. if (tmp_evt->cmnd_done)
  1211. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1212. else if (tmp_evt->done)
  1213. tmp_evt->done(tmp_evt);
  1214. }
  1215. }
  1216. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1217. return SUCCESS;
  1218. }
  1219. /**
  1220. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1221. * @cmd: struct scsi_cmnd having problems
  1222. */
  1223. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1224. {
  1225. unsigned long wait_switch = 0;
  1226. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1227. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1228. ibmvscsi_reset_host(hostdata);
  1229. for (wait_switch = jiffies + (init_timeout * HZ);
  1230. time_before(jiffies, wait_switch) &&
  1231. atomic_read(&hostdata->request_limit) < 2;) {
  1232. msleep(10);
  1233. }
  1234. if (atomic_read(&hostdata->request_limit) <= 0)
  1235. return FAILED;
  1236. return SUCCESS;
  1237. }
  1238. /**
  1239. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1240. * @crq: Command/Response queue
  1241. * @hostdata: ibmvscsi_host_data of host
  1242. *
  1243. */
  1244. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1245. struct ibmvscsi_host_data *hostdata)
  1246. {
  1247. long rc;
  1248. unsigned long flags;
  1249. struct srp_event_struct *evt_struct =
  1250. (struct srp_event_struct *)crq->IU_data_ptr;
  1251. switch (crq->valid) {
  1252. case 0xC0: /* initialization */
  1253. switch (crq->format) {
  1254. case 0x01: /* Initialization message */
  1255. dev_info(hostdata->dev, "partner initialized\n");
  1256. /* Send back a response */
  1257. if ((rc = ibmvscsi_ops->send_crq(hostdata,
  1258. 0xC002000000000000LL, 0)) == 0) {
  1259. /* Now login */
  1260. init_adapter(hostdata);
  1261. } else {
  1262. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1263. }
  1264. break;
  1265. case 0x02: /* Initialization response */
  1266. dev_info(hostdata->dev, "partner initialization complete\n");
  1267. /* Now login */
  1268. init_adapter(hostdata);
  1269. break;
  1270. default:
  1271. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1272. }
  1273. return;
  1274. case 0xFF: /* Hypervisor telling us the connection is closed */
  1275. scsi_block_requests(hostdata->host);
  1276. atomic_set(&hostdata->request_limit, 0);
  1277. if (crq->format == 0x06) {
  1278. /* We need to re-setup the interpartition connection */
  1279. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1280. hostdata->client_migrated = 1;
  1281. hostdata->reenable_crq = 1;
  1282. purge_requests(hostdata, DID_REQUEUE);
  1283. wake_up(&hostdata->work_wait_q);
  1284. } else {
  1285. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1286. crq->format);
  1287. ibmvscsi_reset_host(hostdata);
  1288. }
  1289. return;
  1290. case 0x80: /* real payload */
  1291. break;
  1292. default:
  1293. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1294. crq->valid);
  1295. return;
  1296. }
  1297. /* The only kind of payload CRQs we should get are responses to
  1298. * things we send. Make sure this response is to something we
  1299. * actually sent
  1300. */
  1301. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1302. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1303. (void *)crq->IU_data_ptr);
  1304. return;
  1305. }
  1306. if (atomic_read(&evt_struct->free)) {
  1307. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1308. (void *)crq->IU_data_ptr);
  1309. return;
  1310. }
  1311. if (crq->format == VIOSRP_SRP_FORMAT)
  1312. atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
  1313. &hostdata->request_limit);
  1314. del_timer(&evt_struct->timer);
  1315. if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
  1316. evt_struct->cmnd->result = DID_ERROR << 16;
  1317. if (evt_struct->done)
  1318. evt_struct->done(evt_struct);
  1319. else
  1320. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1321. /*
  1322. * Lock the host_lock before messing with these structures, since we
  1323. * are running in a task context
  1324. */
  1325. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1326. list_del(&evt_struct->list);
  1327. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1328. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1329. }
  1330. /**
  1331. * ibmvscsi_get_host_config: Send the command to the server to get host
  1332. * configuration data. The data is opaque to us.
  1333. */
  1334. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1335. unsigned char *buffer, int length)
  1336. {
  1337. struct viosrp_host_config *host_config;
  1338. struct srp_event_struct *evt_struct;
  1339. unsigned long flags;
  1340. dma_addr_t addr;
  1341. int rc;
  1342. evt_struct = get_event_struct(&hostdata->pool);
  1343. if (!evt_struct) {
  1344. dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
  1345. return -1;
  1346. }
  1347. init_event_struct(evt_struct,
  1348. sync_completion,
  1349. VIOSRP_MAD_FORMAT,
  1350. info_timeout);
  1351. host_config = &evt_struct->iu.mad.host_config;
  1352. /* Set up a lun reset SRP command */
  1353. memset(host_config, 0x00, sizeof(*host_config));
  1354. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1355. host_config->common.length = length;
  1356. host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
  1357. length,
  1358. DMA_BIDIRECTIONAL);
  1359. if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
  1360. if (!firmware_has_feature(FW_FEATURE_CMO))
  1361. dev_err(hostdata->dev,
  1362. "dma_mapping error getting host config\n");
  1363. free_event_struct(&hostdata->pool, evt_struct);
  1364. return -1;
  1365. }
  1366. init_completion(&evt_struct->comp);
  1367. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1368. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  1369. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1370. if (rc == 0)
  1371. wait_for_completion(&evt_struct->comp);
  1372. dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
  1373. return rc;
  1374. }
  1375. /**
  1376. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1377. * @sdev: struct scsi_device device to configure
  1378. *
  1379. * Enable allow_restart for a device if it is a disk. Adjust the
  1380. * queue_depth here also as is required by the documentation for
  1381. * struct scsi_host_template.
  1382. */
  1383. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1384. {
  1385. struct Scsi_Host *shost = sdev->host;
  1386. unsigned long lock_flags = 0;
  1387. spin_lock_irqsave(shost->host_lock, lock_flags);
  1388. if (sdev->type == TYPE_DISK) {
  1389. sdev->allow_restart = 1;
  1390. blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
  1391. }
  1392. scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
  1393. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1394. return 0;
  1395. }
  1396. /**
  1397. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1398. * @sdev: scsi device struct
  1399. * @qdepth: depth to set
  1400. * @reason: calling context
  1401. *
  1402. * Return value:
  1403. * actual depth set
  1404. **/
  1405. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth,
  1406. int reason)
  1407. {
  1408. if (reason != SCSI_QDEPTH_DEFAULT)
  1409. return -EOPNOTSUPP;
  1410. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1411. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1412. scsi_adjust_queue_depth(sdev, 0, qdepth);
  1413. return sdev->queue_depth;
  1414. }
  1415. /* ------------------------------------------------------------
  1416. * sysfs attributes
  1417. */
  1418. static ssize_t show_host_vhost_loc(struct device *dev,
  1419. struct device_attribute *attr, char *buf)
  1420. {
  1421. struct Scsi_Host *shost = class_to_shost(dev);
  1422. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1423. int len;
  1424. len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
  1425. hostdata->caps.loc);
  1426. return len;
  1427. }
  1428. static struct device_attribute ibmvscsi_host_vhost_loc = {
  1429. .attr = {
  1430. .name = "vhost_loc",
  1431. .mode = S_IRUGO,
  1432. },
  1433. .show = show_host_vhost_loc,
  1434. };
  1435. static ssize_t show_host_vhost_name(struct device *dev,
  1436. struct device_attribute *attr, char *buf)
  1437. {
  1438. struct Scsi_Host *shost = class_to_shost(dev);
  1439. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1440. int len;
  1441. len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
  1442. hostdata->caps.name);
  1443. return len;
  1444. }
  1445. static struct device_attribute ibmvscsi_host_vhost_name = {
  1446. .attr = {
  1447. .name = "vhost_name",
  1448. .mode = S_IRUGO,
  1449. },
  1450. .show = show_host_vhost_name,
  1451. };
  1452. static ssize_t show_host_srp_version(struct device *dev,
  1453. struct device_attribute *attr, char *buf)
  1454. {
  1455. struct Scsi_Host *shost = class_to_shost(dev);
  1456. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1457. int len;
  1458. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1459. hostdata->madapter_info.srp_version);
  1460. return len;
  1461. }
  1462. static struct device_attribute ibmvscsi_host_srp_version = {
  1463. .attr = {
  1464. .name = "srp_version",
  1465. .mode = S_IRUGO,
  1466. },
  1467. .show = show_host_srp_version,
  1468. };
  1469. static ssize_t show_host_partition_name(struct device *dev,
  1470. struct device_attribute *attr,
  1471. char *buf)
  1472. {
  1473. struct Scsi_Host *shost = class_to_shost(dev);
  1474. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1475. int len;
  1476. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1477. hostdata->madapter_info.partition_name);
  1478. return len;
  1479. }
  1480. static struct device_attribute ibmvscsi_host_partition_name = {
  1481. .attr = {
  1482. .name = "partition_name",
  1483. .mode = S_IRUGO,
  1484. },
  1485. .show = show_host_partition_name,
  1486. };
  1487. static ssize_t show_host_partition_number(struct device *dev,
  1488. struct device_attribute *attr,
  1489. char *buf)
  1490. {
  1491. struct Scsi_Host *shost = class_to_shost(dev);
  1492. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1493. int len;
  1494. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1495. hostdata->madapter_info.partition_number);
  1496. return len;
  1497. }
  1498. static struct device_attribute ibmvscsi_host_partition_number = {
  1499. .attr = {
  1500. .name = "partition_number",
  1501. .mode = S_IRUGO,
  1502. },
  1503. .show = show_host_partition_number,
  1504. };
  1505. static ssize_t show_host_mad_version(struct device *dev,
  1506. struct device_attribute *attr, char *buf)
  1507. {
  1508. struct Scsi_Host *shost = class_to_shost(dev);
  1509. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1510. int len;
  1511. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1512. hostdata->madapter_info.mad_version);
  1513. return len;
  1514. }
  1515. static struct device_attribute ibmvscsi_host_mad_version = {
  1516. .attr = {
  1517. .name = "mad_version",
  1518. .mode = S_IRUGO,
  1519. },
  1520. .show = show_host_mad_version,
  1521. };
  1522. static ssize_t show_host_os_type(struct device *dev,
  1523. struct device_attribute *attr, char *buf)
  1524. {
  1525. struct Scsi_Host *shost = class_to_shost(dev);
  1526. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1527. int len;
  1528. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1529. return len;
  1530. }
  1531. static struct device_attribute ibmvscsi_host_os_type = {
  1532. .attr = {
  1533. .name = "os_type",
  1534. .mode = S_IRUGO,
  1535. },
  1536. .show = show_host_os_type,
  1537. };
  1538. static ssize_t show_host_config(struct device *dev,
  1539. struct device_attribute *attr, char *buf)
  1540. {
  1541. struct Scsi_Host *shost = class_to_shost(dev);
  1542. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1543. /* returns null-terminated host config data */
  1544. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1545. return strlen(buf);
  1546. else
  1547. return 0;
  1548. }
  1549. static struct device_attribute ibmvscsi_host_config = {
  1550. .attr = {
  1551. .name = "config",
  1552. .mode = S_IRUGO,
  1553. },
  1554. .show = show_host_config,
  1555. };
  1556. static struct device_attribute *ibmvscsi_attrs[] = {
  1557. &ibmvscsi_host_vhost_loc,
  1558. &ibmvscsi_host_vhost_name,
  1559. &ibmvscsi_host_srp_version,
  1560. &ibmvscsi_host_partition_name,
  1561. &ibmvscsi_host_partition_number,
  1562. &ibmvscsi_host_mad_version,
  1563. &ibmvscsi_host_os_type,
  1564. &ibmvscsi_host_config,
  1565. NULL
  1566. };
  1567. /* ------------------------------------------------------------
  1568. * SCSI driver registration
  1569. */
  1570. static struct scsi_host_template driver_template = {
  1571. .module = THIS_MODULE,
  1572. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1573. .proc_name = "ibmvscsi",
  1574. .queuecommand = ibmvscsi_queuecommand,
  1575. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1576. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1577. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1578. .slave_configure = ibmvscsi_slave_configure,
  1579. .change_queue_depth = ibmvscsi_change_queue_depth,
  1580. .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
  1581. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1582. .this_id = -1,
  1583. .sg_tablesize = SG_ALL,
  1584. .use_clustering = ENABLE_CLUSTERING,
  1585. .shost_attrs = ibmvscsi_attrs,
  1586. };
  1587. /**
  1588. * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
  1589. *
  1590. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1591. *
  1592. * Return value:
  1593. * Number of bytes of IO data the driver will need to perform well.
  1594. */
  1595. static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  1596. {
  1597. /* iu_storage data allocated in initialize_event_pool */
  1598. unsigned long desired_io = max_events * sizeof(union viosrp_iu);
  1599. /* add io space for sg data */
  1600. desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
  1601. IBMVSCSI_CMDS_PER_LUN_DEFAULT);
  1602. return desired_io;
  1603. }
  1604. static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
  1605. {
  1606. int rc;
  1607. char *action = "reset";
  1608. if (hostdata->reset_crq) {
  1609. smp_rmb();
  1610. hostdata->reset_crq = 0;
  1611. rc = ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata);
  1612. if (!rc)
  1613. rc = ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0);
  1614. vio_enable_interrupts(to_vio_dev(hostdata->dev));
  1615. } else if (hostdata->reenable_crq) {
  1616. smp_rmb();
  1617. action = "enable";
  1618. rc = ibmvscsi_ops->reenable_crq_queue(&hostdata->queue, hostdata);
  1619. hostdata->reenable_crq = 0;
  1620. if (!rc)
  1621. rc = ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0);
  1622. } else
  1623. return;
  1624. if (rc) {
  1625. atomic_set(&hostdata->request_limit, -1);
  1626. dev_err(hostdata->dev, "error after %s\n", action);
  1627. }
  1628. scsi_unblock_requests(hostdata->host);
  1629. }
  1630. static int ibmvscsi_work_to_do(struct ibmvscsi_host_data *hostdata)
  1631. {
  1632. if (kthread_should_stop())
  1633. return 1;
  1634. else if (hostdata->reset_crq) {
  1635. smp_rmb();
  1636. return 1;
  1637. } else if (hostdata->reenable_crq) {
  1638. smp_rmb();
  1639. return 1;
  1640. }
  1641. return 0;
  1642. }
  1643. static int ibmvscsi_work(void *data)
  1644. {
  1645. struct ibmvscsi_host_data *hostdata = data;
  1646. int rc;
  1647. set_user_nice(current, -20);
  1648. while (1) {
  1649. rc = wait_event_interruptible(hostdata->work_wait_q,
  1650. ibmvscsi_work_to_do(hostdata));
  1651. BUG_ON(rc);
  1652. if (kthread_should_stop())
  1653. break;
  1654. ibmvscsi_do_work(hostdata);
  1655. }
  1656. return 0;
  1657. }
  1658. /**
  1659. * Called by bus code for each adapter
  1660. */
  1661. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1662. {
  1663. struct ibmvscsi_host_data *hostdata;
  1664. struct Scsi_Host *host;
  1665. struct device *dev = &vdev->dev;
  1666. struct srp_rport_identifiers ids;
  1667. struct srp_rport *rport;
  1668. unsigned long wait_switch = 0;
  1669. int rc;
  1670. dev_set_drvdata(&vdev->dev, NULL);
  1671. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1672. if (!host) {
  1673. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1674. goto scsi_host_alloc_failed;
  1675. }
  1676. host->transportt = ibmvscsi_transport_template;
  1677. hostdata = shost_priv(host);
  1678. memset(hostdata, 0x00, sizeof(*hostdata));
  1679. INIT_LIST_HEAD(&hostdata->sent);
  1680. init_waitqueue_head(&hostdata->work_wait_q);
  1681. hostdata->host = host;
  1682. hostdata->dev = dev;
  1683. atomic_set(&hostdata->request_limit, -1);
  1684. hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
  1685. if (map_persist_bufs(hostdata)) {
  1686. dev_err(&vdev->dev, "couldn't map persistent buffers\n");
  1687. goto persist_bufs_failed;
  1688. }
  1689. hostdata->work_thread = kthread_run(ibmvscsi_work, hostdata, "%s_%d",
  1690. "ibmvscsi", host->host_no);
  1691. if (IS_ERR(hostdata->work_thread)) {
  1692. dev_err(&vdev->dev, "couldn't initialize kthread. rc=%ld\n",
  1693. PTR_ERR(hostdata->work_thread));
  1694. goto init_crq_failed;
  1695. }
  1696. rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
  1697. if (rc != 0 && rc != H_RESOURCE) {
  1698. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1699. goto kill_kthread;
  1700. }
  1701. if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
  1702. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1703. goto init_pool_failed;
  1704. }
  1705. host->max_lun = 8;
  1706. host->max_id = max_id;
  1707. host->max_channel = max_channel;
  1708. host->max_cmd_len = 16;
  1709. if (scsi_add_host(hostdata->host, hostdata->dev))
  1710. goto add_host_failed;
  1711. /* we don't have a proper target_port_id so let's use the fake one */
  1712. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1713. sizeof(ids.port_id));
  1714. ids.roles = SRP_RPORT_ROLE_TARGET;
  1715. rport = srp_rport_add(host, &ids);
  1716. if (IS_ERR(rport))
  1717. goto add_srp_port_failed;
  1718. /* Try to send an initialization message. Note that this is allowed
  1719. * to fail if the other end is not acive. In that case we don't
  1720. * want to scan
  1721. */
  1722. if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  1723. || rc == H_RESOURCE) {
  1724. /*
  1725. * Wait around max init_timeout secs for the adapter to finish
  1726. * initializing. When we are done initializing, we will have a
  1727. * valid request_limit. We don't want Linux scanning before
  1728. * we are ready.
  1729. */
  1730. for (wait_switch = jiffies + (init_timeout * HZ);
  1731. time_before(jiffies, wait_switch) &&
  1732. atomic_read(&hostdata->request_limit) < 2;) {
  1733. msleep(10);
  1734. }
  1735. /* if we now have a valid request_limit, initiate a scan */
  1736. if (atomic_read(&hostdata->request_limit) > 0)
  1737. scsi_scan_host(host);
  1738. }
  1739. dev_set_drvdata(&vdev->dev, hostdata);
  1740. return 0;
  1741. add_srp_port_failed:
  1742. scsi_remove_host(hostdata->host);
  1743. add_host_failed:
  1744. release_event_pool(&hostdata->pool, hostdata);
  1745. init_pool_failed:
  1746. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
  1747. kill_kthread:
  1748. kthread_stop(hostdata->work_thread);
  1749. init_crq_failed:
  1750. unmap_persist_bufs(hostdata);
  1751. persist_bufs_failed:
  1752. scsi_host_put(host);
  1753. scsi_host_alloc_failed:
  1754. return -1;
  1755. }
  1756. static int ibmvscsi_remove(struct vio_dev *vdev)
  1757. {
  1758. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
  1759. unmap_persist_bufs(hostdata);
  1760. release_event_pool(&hostdata->pool, hostdata);
  1761. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
  1762. max_events);
  1763. kthread_stop(hostdata->work_thread);
  1764. srp_remove_host(hostdata->host);
  1765. scsi_remove_host(hostdata->host);
  1766. scsi_host_put(hostdata->host);
  1767. return 0;
  1768. }
  1769. /**
  1770. * ibmvscsi_resume: Resume from suspend
  1771. * @dev: device struct
  1772. *
  1773. * We may have lost an interrupt across suspend/resume, so kick the
  1774. * interrupt handler
  1775. */
  1776. static int ibmvscsi_resume(struct device *dev)
  1777. {
  1778. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(dev);
  1779. return ibmvscsi_ops->resume(hostdata);
  1780. }
  1781. /**
  1782. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1783. * support.
  1784. */
  1785. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1786. {"vscsi", "IBM,v-scsi"},
  1787. { "", "" }
  1788. };
  1789. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1790. static struct dev_pm_ops ibmvscsi_pm_ops = {
  1791. .resume = ibmvscsi_resume
  1792. };
  1793. static struct vio_driver ibmvscsi_driver = {
  1794. .id_table = ibmvscsi_device_table,
  1795. .probe = ibmvscsi_probe,
  1796. .remove = ibmvscsi_remove,
  1797. .get_desired_dma = ibmvscsi_get_desired_dma,
  1798. .driver = {
  1799. .name = "ibmvscsi",
  1800. .owner = THIS_MODULE,
  1801. .pm = &ibmvscsi_pm_ops,
  1802. }
  1803. };
  1804. static struct srp_function_template ibmvscsi_transport_functions = {
  1805. };
  1806. int __init ibmvscsi_module_init(void)
  1807. {
  1808. int ret;
  1809. /* Ensure we have two requests to do error recovery */
  1810. driver_template.can_queue = max_requests;
  1811. max_events = max_requests + 2;
  1812. if (firmware_has_feature(FW_FEATURE_ISERIES))
  1813. ibmvscsi_ops = &iseriesvscsi_ops;
  1814. else if (firmware_has_feature(FW_FEATURE_VIO))
  1815. ibmvscsi_ops = &rpavscsi_ops;
  1816. else
  1817. return -ENODEV;
  1818. ibmvscsi_transport_template =
  1819. srp_attach_transport(&ibmvscsi_transport_functions);
  1820. if (!ibmvscsi_transport_template)
  1821. return -ENOMEM;
  1822. ret = vio_register_driver(&ibmvscsi_driver);
  1823. if (ret)
  1824. srp_release_transport(ibmvscsi_transport_template);
  1825. return ret;
  1826. }
  1827. void __exit ibmvscsi_module_exit(void)
  1828. {
  1829. vio_unregister_driver(&ibmvscsi_driver);
  1830. srp_release_transport(ibmvscsi_transport_template);
  1831. }
  1832. module_init(ibmvscsi_module_init);
  1833. module_exit(ibmvscsi_module_exit);