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