ibmvscsi.c 60 KB

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